This is a discussion on PHP & Memory Allocation (checked via TOP) within the PHP General forums, part of the PHP Programming Forums category; Hi, I have written a simple test program to see how php allocates memory. Test code allocates ~10 Meg of ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I have written a simple test program to see how php allocates memory. Test code allocates ~10 Meg of RAM in an array within a loop till it runs out of memory as: $str = rand(65, 95) . rand(65, 95) . rand(65, 95) . rand(65, 95) . rand(65, 95); $aa[] = str_repeat($str, 2000000); What I don't understand here is, for every 10 Meg memory it allocates, mem usage goes up about 19 Megs when I look at this via top. Basically, it allocates from physical memory in the beginning, starts using swap when it is out of RES/Physical memory, which makes me assume that garbage collection should kick in for temporary string creations, and each new allocation increases the allocated memory size by 19 Megs. Any idea why this is happening, and why do you think memory allocation is so expensive in PHP? Thanks.. Results of TOP: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 21843 apache 17 0 19292 5232 3832 S 0.0 0.5 0:00.04 php 21843 apache 16 0 38824 24m 3908 S 0.0 2.4 0:00.10 php 21843 apache 15 0 58356 43m 3912 S 0.0 4.3 0:00.17 php 21843 apache 16 0 77888 62m 3912 S 0.0 6.2 0:00.22 php 21843 apache 15 0 97420 81m 3912 S 0.0 8.1 0:00.29 php 21843 apache 15 0 114m 100m 3912 S 0.0 9.9 0:00.35 php Results of free -m: total used free shared buffers cached Mem: 1011 138 872 0 2 58 Swap: 2008 388 1619 Mem: 1011 158 852 0 2 58 Swap: 2008 388 1619 Mem: 1011 177 833 0 2 58 Swap: 2008 388 1619 Mem: 1011 196 814 0 2 58 Swap: 2008 388 1619 Mem: 1011 216 795 0 2 58 Swap: 2008 388 1619 Mem: 1011 234 776 0 2 58 Swap: 2008 388 1619 |
|
|||
|
Cabbar Duzayak wrote:
> Hi, > > I have written a simple test program to see how php allocates memory. > Test code allocates ~10 Meg of RAM in an array within a loop till it > runs out of memory as: > > $str = rand(65, 95) . rand(65, 95) . rand(65, 95) . rand(65, 95) . > rand(65, 95); > $aa[] = str_repeat($str, 2000000); > > What I don't understand here is, for every 10 Meg memory it allocates, > mem usage goes up about 19 Megs when I look at this via top. Just looked over this really quickly, so I might have missed something, but it seems to me that you're allocating almost 20 megs... your $str is 10 bytes long (plus terminator and PHP overhead, whatever that turns out to be...), and you're repeating it two million times. 10 bytes times 2 million is 20 million bytes. 20 000 000 divided by (1024*1024) is 19.07 -> it seems to me that you're allocating 19.07 megs, and top is showing you that it's allocating 19 megs...? Seems about right. jon |
|
|||
|
$str is 10 bytes
then you repeat it 2000000 times That gives you 20000000 bytes. That's 20M not 10M -Rasmus Cabbar Duzayak wrote: > Hi, > > I have written a simple test program to see how php allocates memory. > Test code allocates ~10 Meg of RAM in an array within a loop till it > runs out of memory as: > > $str = rand(65, 95) . rand(65, 95) . rand(65, 95) . rand(65, 95) . > rand(65, 95); > $aa[] = str_repeat($str, 2000000); > > What I don't understand here is, for every 10 Meg memory it allocates, > mem usage goes up about 19 Megs when I look at this via top. > Basically, it allocates from physical memory in the beginning, starts > using swap when it is out of RES/Physical memory, which makes me > assume that garbage collection should kick in for temporary string > creations, and each new allocation increases the allocated memory size > by 19 Megs. > > Any idea why this is happening, and why do you think memory allocation > is so expensive in PHP? > > Thanks.. > > > Results of TOP: > > PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND > 21843 apache 17 0 19292 5232 3832 S 0.0 0.5 0:00.04 php > 21843 apache 16 0 38824 24m 3908 S 0.0 2.4 0:00.10 php > 21843 apache 15 0 58356 43m 3912 S 0.0 4.3 0:00.17 php > 21843 apache 16 0 77888 62m 3912 S 0.0 6.2 0:00.22 php > 21843 apache 15 0 97420 81m 3912 S 0.0 8.1 0:00.29 php > 21843 apache 15 0 114m 100m 3912 S 0.0 9.9 0:00.35 php > > > Results of free -m: > > total used free shared buffers cached > Mem: 1011 138 872 0 2 58 > Swap: 2008 388 1619 > > Mem: 1011 158 852 0 2 58 > Swap: 2008 388 1619 > > Mem: 1011 177 833 0 2 58 > Swap: 2008 388 1619 > > Mem: 1011 196 814 0 2 58 > Swap: 2008 388 1619 > > Mem: 1011 216 795 0 2 58 > Swap: 2008 388 1619 > > Mem: 1011 234 776 0 2 58 > Swap: 2008 388 1619 > |