PHP & Memory Allocation (checked via TOP)

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-31-2006
Cabbar Duzayak
 
Posts: n/a
Default PHP & Memory Allocation (checked via TOP)

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
Reply With Quote
  #2 (permalink)  
Old 10-31-2006
Cabbar Duzayak
 
Posts: n/a
Default Re: PHP & Memory Allocation (checked via TOP)

As a side note, even though user is shown as apache, this code was
executed from command line.
Reply With Quote
  #3 (permalink)  
Old 10-31-2006
Jon Anderson
 
Posts: n/a
Default Re: [PHP] PHP & Memory Allocation (checked via TOP)

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
Reply With Quote
  #4 (permalink)  
Old 11-01-2006
Rasmus Lerdorf
 
Posts: n/a
Default Re: [PHP] PHP & Memory Allocation (checked via TOP)

$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
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:17 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0