phdphd Posted September 7, 2013 Share Posted September 7, 2013 Hi all,I have a PHP script that connects to a db, retrieves some data and displays it to the user.To get an idea of memory usage, I inserted some "echo memory_get_usage();" statements at different stages at the very beginning of the script, after the execution of the query, after the creation of an array gathering the data retrieved, just before the end of script, and finally just after the end of script.As one may expect, the memory usage gets higher and higher as the script is running. However I do not undersand why the memory usage remains at its highest level at the last stage (after the end of the script), and does not get freed for further processing.Thanks for shedding some light. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 7, 2013 Share Posted September 7, 2013 How exactly are you determining the memory usage after the end of the php script? Quote Link to comment Share on other sites More sharing options...
phdphd Posted September 7, 2013 Author Share Posted September 7, 2013 I added another php script with just "echo memory_get_usage();" in it. <?php echo memory_get_usage(); ?> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 7, 2013 Share Posted September 7, 2013 (edited) you do realize that the php script didn't end. it ends when the very last statement on the page has finished running, be it an actual php statement or some in-line html. what sort of problem are you trying to find and solve? Edited September 7, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
phdphd Posted September 7, 2013 Author Share Posted September 7, 2013 Thanks for your cooperation Guru. The page presents various groups of data to the user. Each time one group of data has been displayed I would like PHP to free the memory used and dedicate it to the display of the next group of data. When there is no much data to display, there is no problem. However I would like to avoid the "allowed memory size of XXXX bytes exhausted" message. Or may be an alternative could be implementing search boxes instead of groups that are likely to grow too much over time. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 7, 2013 Share Posted September 7, 2013 without knowing what your code is doing, there's no way to answer your question. the method that's using the memory, determines how to free up the memory when it's done or of using a different method that doesn't consume as much memory in the first place. Quote Link to comment Share on other sites More sharing options...
vinny42 Posted September 7, 2013 Share Posted September 7, 2013 Each time one group of data has been displayed I would like PHP to free the memory used and dedicate it to the display of the next group of data. Like McGyver said you'd have to tell more about the code, but... Once a group of data has been displayed you no longer need the variables that hold that group, so you can simply re-use the exact same variables, which will also reuse the memory. Roughly speaking, this will use a maximum of 10 bytes at any given time: $a = 'hello'; echo $a; $a ' ='1234567890'; echo $a; $a = 'Goodbye'; echo $a; I have assigned 22 bytes to $a, but only a maximum of 10 at a time, so PHP will use 5 bytes for the first string, add 5 for the second, and release 3 bytes for the last string. Quote Link to comment Share on other sites More sharing options...
Irate Posted September 7, 2013 Share Posted September 7, 2013 If you are using MySQL(i) Objects and are firing queries, you need to call mysqli::close() to release the memory it occupied once you are done with the query. That might be a cause of the problem. As for general memory space, I think that PHP has this feature named dynamic memory size, it creates and deletes memory space "on the fly", otherwise the whole principle of untyped variables would not quite work properly. Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 8, 2013 Solution Share Posted September 8, 2013 @Irate any database close() statement just closes the database connection. that will have little affect on memory usage. any result sets that may have been retrieved and any follow on data produced from processing the result set(s) will still exist. as to your second statement, untyped variables have nothing at all to do with memory usage or how memory is managed. until the OP provides specific information about what his code is really doing leading up to the point where the memory error is occurring at, this thread is just for bumping up post counts. for all we know, he's dynamically producing images/graphs from the data. Quote Link to comment Share on other sites More sharing options...
vinny42 Posted September 8, 2013 Share Posted September 8, 2013 you need to call mysqli::close() to release the memory it occupied once you are done with the query You're thinking of mysql_free_result() untill then this thread is just for bumping up post counts Pleas tell me this forum is grown-up enough not to care about postcounts.... Quote Link to comment Share on other sites More sharing options...
requinix Posted September 8, 2013 Share Posted September 8, 2013 Pleas tell me this forum is grown-up enough not to care about postcounts....We'll stop caring when you finally post the code you're having problems with. Quote Link to comment Share on other sites More sharing options...
vinny42 Posted September 8, 2013 Share Posted September 8, 2013 We'll stop caring when you finally post the code you're having problems with. I can't, I'm not the one having the problem :-) Quote Link to comment Share on other sites More sharing options...
requinix Posted September 8, 2013 Share Posted September 8, 2013 I can't, I'm not the one having the problem :-)>_> You need an avatar. Quote Link to comment Share on other sites More sharing options...
phdphd Posted September 10, 2013 Author Share Posted September 10, 2013 (edited) Hi All, sorry for my late reply. any result sets that may have been retrieved and any follow on data produced from processing the result set(s) will still exist. This seems to be the key to my issue. By unsetting variables that are no longer needed at each step, I managed to recovery a significant amount of memory. Thank you Mac_gyver! Edited September 10, 2013 by phdphd Quote Link to comment 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.