Jump to content

Setting to null doesn't free memory space ?


myrddinwylt

Recommended Posts

Hello,

 

I have some blocks of code which loop through a database of customers, then for each customer, their phone numbers, and services, and generates a PDF file using TCPDF.  The code is working and functional and generates the proper output.  Inside the each loop, when finished with variables and objects, I cleanup to the best of my ability by setting them to "null" --- for example:

 

$obj = null;

 

Prior to that, I also close any open database connections. "mysql_free_result($result);"  doesn't work at all and produces an error, so I do the following:

$result = null;
mysql_close($cn);
$cn = null;

 

After each PDF is output, I set the object to null as well, and well... you get the idea, after everything, I incorporate cleanup (always a good habit).

 

Now the problem.  After around 400-500 records, php runs into a memory exhausted limit issue.  If I increase the limit, I can get a few more invoices created, but really, php should be cleaning up this crap.  How can I best ensure inside the same loop, that cleanup occurs on the variables after closing objects / variables, and setting them to null ---- or is this an inherant flaw in php where garbage cleanup simply does not exist ?.

 

I do not wish to output anything to the screen during this process. The timeout is set for long enough to generate the PDF's, and the entire process shouldn't take longer than around 10-15 minutes.

 

Thanks in advance.

Link to comment
Share on other sites

Thank you for the response joel24.

 

Just a couple quick questions about this.

 

Will "unset()" work with objects as well as variables?

Does "unset()" completly release the memory that object used? (meaning, if you look in taskmanager, will you see the actual memory in use of PHP drop after this function is called -- proving the memory has in fact been completely freed)

Will "unset()" work recursively?  (aka  .... )

$x = 0;
while($x < 3) {
  $myobj[] = new object();
  $x++;
}

unset($myobj);

 

 

Link to comment
Share on other sites

Thank you guys for the responses.

 

I guess based on this information, PHP does not have any garbage cleanup, so simply unsetting an object, or setting the object to null doesn't free it's memory space.  Further, return variables also are not automatically cleaned such as the following:

 

function somefunction() {
    $var1 = "hello world";
    return $var1;
}

$var2 = somefunction();

 

The above code would end up using double the memory space, as the function "somefunction()" contains a variable which can not be "unset()", etc, as it must return the value as the functions result. Upon returning that value, $var1 is not cleaned up, and permanently consuming memory.  Then $var2 which is active variable that stores the result of "somefunction()", is further consuming at least the memory space of the return value -- in this case "hello world".

 

This to me, is a severe design flaw with the language itself, and must be addressed. Garbage cleanup is necessary, and should not be left up to the OS.  So either this language is fundamentally flawed at it's source, or the information that is available (through responses here, and on php.net itself), is not accurate.

 

Personally, if my server has 32 GB of memory, I do not wish to run out of memory simply because I looped through 500,000 customers (something even Visual Basic can handle the garbage cleanup for --- or... comparability:  ASP / ASP.NET) .

Link to comment
Share on other sites

Upon returning that value, $var1 is not cleaned up, and permanently consuming memory

 

Did you determine that through testing, because AFAIK local function variables (unless declared as static) are destroyed when the function returns.

 

I also close any open database connections

^^^ That concerns me because constantly opening and closing the database connection probably adds %5 to the processing time of each loop through your script.

 

"mysql_free_result($result);"  doesn't work at all and produces an error

^^^ That implies $result is not a resource, but it would take seeing the actual error and code to tell anything further.

 

Php actually does have built in resource cleanup -

Freeing resources

Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

 

To fix your memory problem (it could be a php bug you are seeing) would require you to determine where the memory leak is at first. Just randomly trying things in your code won't help if the problem is due to an actual bug. Have you logged the memory_get_usage() values to determine where and how much memory is being used and then how much is being freed up or not being freed up?

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.