Jump to content

Why does not PHP free memory ?


phdphd
Go to solution Solved by mac_gyver,

Recommended Posts

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.
 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 


 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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

@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.

Link to comment
Share on other sites

 


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.... 

Link to comment
Share on other sites

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 by phdphd
Link to comment
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.