I was trying to check the memory_get_peak_usage() method, I try to create a objects dynamically inside a for loop, and tried to print the memory used by my php script. here is the code :-
$objArr = array();
for( $i=1; $i<=100; $i++ ){
echo "creating employee == " . memory_get_peak_usage()/1024 . " Kbs\n";
$objArr[$i] = new CEmployee();
}
exit;
And here is the result that confused me :-
creating employee == 4156.84375 Kbs
creating employee == 6619.640625 Kbs
creating employee == 6619.640625 Kbs
creating employee == 6619.640625 Kbs
...
...(up to 40 lines from 2nd line same result as above and then from 41 line changed)
creating employee == 6623.5546875 Kbs
creating employee == 6631.234375 Kbs
creating employee == 6638.9140625 Kbs
creating employee == 6646.59375 Kbs
creating employee == 6654.2734375 Kbs
creating employee == 6661.953125 Kbs
creating employee == 6669.6328125 Kbs
creating employee == 6677.3125 Kbs
creating employee == 6684.9921875 Kbs
....
....
I am not getting how php is allocating memory to the objects. I am running the script on command line interface on windows 7
UPDATED CODE:-
gc_collect_cycles();
for( $i=1; $i<=100; $i++ ){
echo "creating employee == " . memory_get_usage()/1024 . " Kbs\n";
$objArr[$i] = new CEmployee();
$objArr[$i]->setEmployeeNumber($i);
}exit;
As you can see by this it does increase memory usage:
http://codepad.org/RApsrlXh
However you might have data ready for garbage collection, hence when you create a new
CEmployeeit might be using that space.Try running
gc_collect_cycles()before your loop to and usememory_get_usage()to get a more accurate reading.