vintox Posted October 3, 2012 Share Posted October 3, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/ Share on other sites More sharing options...
requinix Posted October 3, 2012 Share Posted October 3, 2012 PHP sometimes does unusual and unexpected things with memory. Try using memory_get_peak_usage() instead. Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382545 Share on other sites More sharing options...
vintox Posted October 3, 2012 Author Share Posted October 3, 2012 (edited) 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 October 3, 2012 by vintox Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382566 Share on other sites More sharing options...
requinix Posted October 3, 2012 Share Posted October 3, 2012 (edited) 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 October 3, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382570 Share on other sites More sharing options...
kicken Posted October 4, 2012 Share Posted October 4, 2012 (edited) 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 October 4, 2012 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382600 Share on other sites More sharing options...
vintox Posted October 4, 2012 Author Share Posted October 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382740 Share on other sites More sharing options...
kicken Posted October 4, 2012 Share Posted October 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382752 Share on other sites More sharing options...
vintox Posted October 5, 2012 Author Share Posted October 5, 2012 ok, Thanks Kicken Quote Link to comment https://forums.phpfreaks.com/topic/269049-weird-situation/#findComment-1382820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.