Php get memory size of variable
You can't retrospectively calculate the exact footprint of a variable as two variables can share the same allocated space in the memory Show Let's try to share memory between two arrays, we see that allocating the second array costs half of the memory of the first one. When we unset the first one, nearly all the memory is still used by the second one.
So we can't conclude than the second array uses half the memory, as it becomes false when we unset the first one. For a full view about how the memory is allocated in PHP and for which use, I suggest you to read the following article: How big are PHP arrays (and values) really? (Hint: BIG!) The Reference Counting Basics in the PHP documentation has also a lot of information about memory use, and references count to shared data segment. The different solutions exposed here are good for approximations but none can handle the subtle management of PHP memory.
If you want the newly allocated space after an assignment, then you have to use
Remember that if you want to store the result of the first If you want to echo like in the above example, your output buffer has to be already opened to avoid accounting memory needed to open the output buffer.
If you want to rely on a function to calculate the required space to store a copy of a variable, the following code takes care of different optimizations:
Note that the size of the variable name matters in the memory allocated.
A variable has a basic size defined by the inner C structure used in the PHP source code. This size does not fluctuate in the case of numbers. For strings, it would add the length of the string.
If we do not take the initialization of the variable name into account, we already know how much a variable uses (in case of numbers and strings):
(those numbers can change depending on the PHP version) You have to round up to a multiple of 4 bytes due to memory alignment. If the variable is in the global space (not inside a function), it will also allocate 64 more bytes. So if you want to use one of the codes inside this page, you have to check that the result using some simple test cases (strings or numbers) match those data taking into account every one of the indications in this post ($_GLOBAL array, first function call, output buffer, ...) (PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8) memory_get_usage — Returns the amount of memory allocated to PHP Descriptionmemory_get_usage(bool Parametersreal_usage Set this to
Return ValuesReturns the memory amount in bytes. ExamplesExample #1 A memory_get_usage() example
memory_get_usage() . "\n"; // 57960unset($a); See Also
xelozz -at- gmail.com ¶ 12 years ago
convert(memory_get_usage(true)); // 123 kb Alex Aulbach ¶ 11 years ago
jeff dot peck at snet dot net ¶ 13 years ago
$mem_usage < 1024)
stanislav dot eckert at vizson dot de ¶ 5 years ago
stristr(PHP_OS, "win")) { randolphothegreat at yahoo dot com ¶ 17 years ago
lorin dot weilenmann at gmail dot com ¶ 11 years ago
yousha dot a at hotmail dot com ¶ 4 years ago
ad-rotator.com ¶ 18 years ago
miami at blackcrystal dot net ¶ 7 years ago
e dot a dot schultz at gmail dot com ¶ 16 years ago
preg_replace( '/[\D]/', '', $output[5] ) * 1024;
vesa dot kivisto at nepton dot fi ¶ 17 years ago
trim(substr($output[$resultRow], $resultRowItemStartPosition, $resultRowItemLength)) . ' KB';
MagicalTux at FF dot st ¶ 17 years ago
guenter_doege at web dot de ¶ 17 years ago
function_exists('memory_get_usage')) joe at schmoe dot com ¶ 15 years ago
miteigi nemoto (miteigi at yandex dot ru) ¶ 13 years ago
How to get memory usage in PHP?The memory_get_usage function can be used to track the memory usage. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally. The above mentioned memory usage can be tracked using memory_get_usage().
How to check PHP memory?How to check your usage of PHP script memory script. memory_get_usage() Although this function gives back the amount of memory allotted to a PHP script, you will see the amount in bytes. ... . memory_get_usage/1024. If you want to convert this amount to MB, use:. memory_get_usage/1048576. ... . memory_get_peak_usage() ... . $real_usage.. Which function is used to detect how much memory the variable uses?Is there a function in PHP (or a PHP extension) to find out how much memory a given variable uses? sizeof just tells me the number of elements/properties. memory_get_usage helps in that it gives me the memory size used by the whole script.
How much memory does each PHP process consume?The default memory limit of 128 MB in PHP is a conservative default intended to give enough “breathing room” for virtually any kind of application. It is likely that your code does not consume that much memory during a request, so lowering the limit is a great way of optimizing your application performance.
|