[PHP] PHP Memory Allocation (checked via TOP)

2006-10-31 Thread Cabbar Duzayak

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, 200);

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 %MEMTIME+  COMMAND
21843 apache17   0 19292 5232 3832 S  0.0  0.5   0:00.04 php
21843 apache16   0 38824  24m 3908 S  0.0  2.4   0:00.10 php
21843 apache15   0 58356  43m 3912 S  0.0  4.3   0:00.17 php
21843 apache16   0 77888  62m 3912 S  0.0  6.2   0:00.22 php
21843 apache15   0 97420  81m 3912 S  0.0  8.1   0:00.29 php
21843 apache15   0  114m 100m 3912 S  0.0  9.9   0:00.35 php


Results of free -m:

total   used   free sharedbuffers cached
Mem:  1011138872  0  2 58
Swap: 2008388   1619

Mem:  1011158852  0  2 58
Swap: 2008388   1619

Mem:  1011177833  0  2 58
Swap: 2008388   1619

Mem:  1011196814  0  2 58
Swap: 2008388   1619

Mem:  1011216795  0  2 58
Swap: 2008388   1619

Mem:  1011234776  0  2 58
Swap: 2008388   1619

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP Memory Allocation (checked via TOP)

2006-10-31 Thread Jon Anderson

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, 200);

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP Memory Allocation (checked via TOP)

2006-10-31 Thread Rasmus Lerdorf
$str is 10 bytes
then you repeat it 200 times
That gives you 2000 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, 200);
 
 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 %MEMTIME+  COMMAND
 21843 apache17   0 19292 5232 3832 S  0.0  0.5   0:00.04 php
 21843 apache16   0 38824  24m 3908 S  0.0  2.4   0:00.10 php
 21843 apache15   0 58356  43m 3912 S  0.0  4.3   0:00.17 php
 21843 apache16   0 77888  62m 3912 S  0.0  6.2   0:00.22 php
 21843 apache15   0 97420  81m 3912 S  0.0  8.1   0:00.29 php
 21843 apache15   0  114m 100m 3912 S  0.0  9.9   0:00.35 php
 
 
 Results of free -m:
 
 total   used   free sharedbuffers cached
 Mem:  1011138872  0  2 58
 Swap: 2008388   1619
 
 Mem:  1011158852  0  2 58
 Swap: 2008388   1619
 
 Mem:  1011177833  0  2 58
 Swap: 2008388   1619
 
 Mem:  1011196814  0  2 58
 Swap: 2008388   1619
 
 Mem:  1011216795  0  2 58
 Swap: 2008388   1619
 
 Mem:  1011234776  0  2 58
 Swap: 2008388   1619
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php