C
C
cybernoob2018-01-31 15:18:15
PHP
cybernoob, 2018-01-31 15:18:15

How to deal with memory in PHP?

There is a code:

echo memory_get_usage();
$a = 'a';
echo memory_get_usage();

Run from web:
217360
217496
(diff 136)
Run from console
221376
221560
(diff 136)
echo memory_get_usage();
$a = 'a';
$b = 'b';
echo memory_get_usage();

217360
217632
(difference 272 = 136*2)
But running with a variable again gives a difference of 136 The function outputs:$a = 'abcdefghjklmnopqr';
Returns the amount of memory, in bytes, that has been allocated to the PHP script so far.

Questions:
1) 217360 is it allocated to the entire Apache thread?
2) 136 bytes of 8 bits each, although I do not have a multibyte encoding like where 1 character = several bytes. Where does 136 come from?
3) Why in the third case where there are many letters again 136? There are 17 characters already, like the third byte should go from memory ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex McArrow, 2018-01-31
@cybernoob

1) per running PHP process
2) Memory is allocated in blocks (with a margin), since PHP is not a strongly typed language, and this variable can (unexpectedly) become a boolean or just an array
3) More likely the increase (which you observe) is not a block reservation memory for data, and the occupied memory for a link to the memory block (in which the data is stored), and possibly for the data themselves, the memory is used from an already reserved area

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question