Forum Moderators: coopster

Message Too Old, No Replies

Massive memory usage by PHP when I assign multidimensional array

         

defireman

9:54 am on May 16, 2006 (gmt 0)

10+ Year Member



I'm writing a php-based image processing utililty, and trying to store the pixel color values in a 2D multidimensional array. If I try to process a larger image (2592px x 1944px), PHP uses 500MB of memory just to store 19683KB of ints. Using array_fill to create an array of similar size also creates a much smaller array.

Here's a sample piece of my code:

$imgplane = array_fill(0, $dwidth, array_fill(0, $dheight, 0));

for ($valx = 0; $valx < $dwidth; $valx++) {
for ($valy = 0; $valy < $dheight; $valy++) {
$rgb = imagecolorat($im, $valx, $valy);
$lum = getLum($rgb);

if ($lum < THRESH_UPPER && $lum > getLum($imgplane[$valx][$valy])) {
$imgplane[$valx][$valy] = $rgb;
}
}
}

Does anyone know why it's using so much memory? Is ther e a way to reduce the massive memory usage? Thanks!

eelixduppy

10:12 pm on May 16, 2006 (gmt 0)



You are attempting to create massive arrays which are eating up your memory. I would suggest only storing the pixel values that are absolutely necessary. In your given example(2592px x 1944px), you are creating an array of 5,038,848 values. This is a gigantic array of DOOM! Try to minimize it to the necessary.

defireman

10:41 pm on May 16, 2006 (gmt 0)

10+ Year Member



Yes, I know the array is massive, but even with 5 million 4 byte ints, technically the array should only take up 20MB of memory, not 550MB? So why is PHP eating that much extra memory just to store 20MB of data?

Is it a memory leak or something?

(I've calculated that PHP used ~109bytes of memory to store an Int, which is 4 bytes. That's way too much memory to account for internal structs...)