Jump to content

Recommended Posts

I am developing a system using the MVC design pattern.

for debugging & optimizing the script i am using

memory_get_usage()

at the Start and the End of the script.

 

i have a file footer.html.php

if the file looks like this

<?= $bodyContent ?>

 

by using extract with

$bodyContent = \Views\General_View::pageFooter();

the script consume 76kb of memory.

 

but if i change footer.html.php to

<?= \Views\General_View::pageFooter() ?>

the script consume 108kb of memory.

 

pageFooter method is:

static public function pageFooter()
{
$str = '<br /><br />'.Converter::memorySizeConverter($b = memory_get_usage()).
' ('.Converter::memorySizeConverter($b-SCRIPT_START_MEMORY).')';
$end = microtime(true);
$str .= '<br />Processing time: '. sprintf('%.4f', ($end-SCRIPT_START_TIME)).' seconds';
return $str;
}

Link to comment
https://forums.phpfreaks.com/topic/269049-weird-situation/
Share on other sites

using memory_get_peak_usage() gives the same results as memory_get_usage

when i use "pageFooter" method. returns 108kb

 

if i use memory_get_usage(true)/memory_get_peak_usage(true)

i get 256kb

even if i add code to my script

still returns 256kb

Edited by vintox
Link to comment
https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382566
Share on other sites

In terms of what I think you want to do, memory_get_peak_usage() might be more appropriate: tells you the maximum amount of memory PHP claimed rather than just what it has at the moment.

 

[edit] No! Code blocks screw up ASCII art... I had a chart showing sample memory usage over time and how memory_get_usage() and memory_get_peak_usage() differ. Oh well. [/edit]

 

Adding $real_usage=true shows you that PHP allocates memory in chunks - it doesn't mean your code is using that much memory but that PHP claimed so much for itself (and will give you parts of that as needed).

 

As for the original question of why it uses more memory, I'm not intimately familiar enough with PHP's memory model to understand. But I could certainly make some fairly-educated guesses.

Edited by requinix
Link to comment
https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382570
Share on other sites

When you call your footer function and assign the result to a variable I assume you are doing this in your controller somewhere, or at least somewhere other than the template file. What that means is your measuring the memory usage at different points so you can't really compare the numbers to each other.

 

Calling the functions in different areas like that is like measuring how bright it is outside at noon vs 5pm. Obviously the numbers will be different because the time frame is different and different amount of stuff has happened.

Edited by kicken
Link to comment
https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382600
Share on other sites

When you call your footer function and assign the result to a variable I assume you are doing this in your controller somewhere, or at least somewhere other than the template file. What that means is your measuring the memory usage at different points so you can't really compare the numbers to each other.

 

Calling the functions in different areas like that is like measuring how bright it is outside at noon vs 5pm. Obviously the numbers will be different because the time frame is different and different amount of stuff has happened.

 

the assigning occurs at the controller. after the assigning i am executing View::render($data);

there i am doing extract.

 

the second method skips the assigning to a variable and the extracting by the view class

and execute the pageFooter method

so logically it should use less memory

 

you mentioned the different times i am executing pageFooter.

if i use memory_get_peak_usage() is it still matter?

Link to comment
https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382740
Share on other sites

memory_get_peak_usage would show you the maximum amount of memory your script consumed at any particular point (prior to the call of the function). It's numbers may change or they may not, depends on whether the process of rendering the view causes PHP to use more memory or not.

 

so logically it should use less memory

 

The process of actually rendering your view would likely use more memory than you'd save by removing that one variable. It all depends on how things are setup.

 

My main point was that you can't gather the memory usage data in two different places and expect the numbers to be the same.

 

 

Link to comment
https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382752
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.