Memory allocation failed 'Cannot allocate memory'
Posted: 2010-09-21T13:44:51-07:00
The setup:
I have a custom Perl script that I use for taking a set of JPG images in a directory and creating an HTML gallery with thumbnails. The script was written years ago and was using "pnmscale" (from the old Netpbm utilities) for scaling the images to thumbnail size. Due to changes on my webserver this program has stopped functioning correctly and in looking for alternatives came across ImageMagick and PerlMagick. NOTE: These images are not huge (mostly in the range of 900x600 pixels).
The problem:
I originally rewrote my "thumnail" script from using something like: "djpeg <infile.jpg | pnmscale -width 150 | cjpeg >outfile.jpg" to use the ImageMagick "convert -thumbnail" functionality. Having tested and found my "thumbnail" script apparently working I started trying to generate galleries. I would get memory allocation errors at random points during the running of the script. I thought it might be due to some sort of permissioning issue on the webserver (with my main gallery generation Perl script calling a separate Perl thumbnail script) and decided to see if using PerlMagick to directly access the ImageMagick functionality through the Perl object oriented interface would work around these problems. No dice.
The code (well the important snippet anyway):
my $image = new Image::Magick;
$image->Read("${file}.jpg");
my ($height, $width) = $image->Get('height', 'width');
if ($height > $width)
{
$width *= 150 / $height;
$height = 150;
}
else
{
$height *= 150 / $width;
$width = 150;
}
$image->Thumbnail(width => $width, height => $height);
$image->write(filename => "${file}_thumb.jpg", compress => "JPEG");
The error:
The script works to a different extent every time I run it. It will process some of the JPG files just fine (resulting in perfectly adequate thumnail images) but bomb on some images in a seemely purely random order. When the script bombs it will throw an error message in the form:
PerlMagick: Memory allocation failed 'Cannot allocate memory @ <module>
Where <module> could be one of any of the following (randomly):
cache.c/AcquirePixelCacheInfo/213
cache.c/AcquirePixelCacheNexus/288
cache.c/AcquirePixelCacheNexus/293
semaphore.c/AllocateSemaphoreInfo/174
exception.c/AcquireExceptionInfo/108
etc....
The version:
ImageMagick 6.4.8 2009-02-30 Q16 OpenMP ....
The things I've tried:
1) I've seen (though web searches) suggestions of limiting the amount of memory allowed (either increasing to allow more memory or decreasing to force it to use memory mapped files instead). No change in the program's random failure point based on a variety of limit values set.
2) Wailing and gnashing of teeth. Not much help their either (and my teeth now hurt).
3) Joining this board and posting this message to see if cooler heads can prevail in finding a solution to this annoying (and random) memory allocation problem. I hate random problems. I hate them even more when they come from a piddling little script that I just want to work so I can get on and do something more useful.
The plea:
HELP!
KM
I have a custom Perl script that I use for taking a set of JPG images in a directory and creating an HTML gallery with thumbnails. The script was written years ago and was using "pnmscale" (from the old Netpbm utilities) for scaling the images to thumbnail size. Due to changes on my webserver this program has stopped functioning correctly and in looking for alternatives came across ImageMagick and PerlMagick. NOTE: These images are not huge (mostly in the range of 900x600 pixels).
The problem:
I originally rewrote my "thumnail" script from using something like: "djpeg <infile.jpg | pnmscale -width 150 | cjpeg >outfile.jpg" to use the ImageMagick "convert -thumbnail" functionality. Having tested and found my "thumbnail" script apparently working I started trying to generate galleries. I would get memory allocation errors at random points during the running of the script. I thought it might be due to some sort of permissioning issue on the webserver (with my main gallery generation Perl script calling a separate Perl thumbnail script) and decided to see if using PerlMagick to directly access the ImageMagick functionality through the Perl object oriented interface would work around these problems. No dice.
The code (well the important snippet anyway):
my $image = new Image::Magick;
$image->Read("${file}.jpg");
my ($height, $width) = $image->Get('height', 'width');
if ($height > $width)
{
$width *= 150 / $height;
$height = 150;
}
else
{
$height *= 150 / $width;
$width = 150;
}
$image->Thumbnail(width => $width, height => $height);
$image->write(filename => "${file}_thumb.jpg", compress => "JPEG");
The error:
The script works to a different extent every time I run it. It will process some of the JPG files just fine (resulting in perfectly adequate thumnail images) but bomb on some images in a seemely purely random order. When the script bombs it will throw an error message in the form:
PerlMagick: Memory allocation failed 'Cannot allocate memory @ <module>
Where <module> could be one of any of the following (randomly):
cache.c/AcquirePixelCacheInfo/213
cache.c/AcquirePixelCacheNexus/288
cache.c/AcquirePixelCacheNexus/293
semaphore.c/AllocateSemaphoreInfo/174
exception.c/AcquireExceptionInfo/108
etc....
The version:
ImageMagick 6.4.8 2009-02-30 Q16 OpenMP ....
The things I've tried:
1) I've seen (though web searches) suggestions of limiting the amount of memory allowed (either increasing to allow more memory or decreasing to force it to use memory mapped files instead). No change in the program's random failure point based on a variety of limit values set.
2) Wailing and gnashing of teeth. Not much help their either (and my teeth now hurt).
3) Joining this board and posting this message to see if cooler heads can prevail in finding a solution to this annoying (and random) memory allocation problem. I hate random problems. I hate them even more when they come from a piddling little script that I just want to work so I can get on and do something more useful.
The plea:
HELP!
KM