Reputation:
Do we really need to free memory by objects we created? Or does PHP handle this by itself?
I've used free()
in c, delete
in c++ so far.
Upvotes: 1
Views: 187
Reputation: 522081
You have no actual control over memory allocation in PHP. PHP allocates memory and frees memory as you create and destroy variables. So: no, you cannot free memory by hand and don't have to either.
Having said that, you have indirect control over the used memory by how many variables you have in scope at any one time/how much data those variables hold. If your script is running into memory issues, you need to optimise what you store where and when those variables are discarded, so PHP can free the associated memory.
For example, this will eat a large amount of memory:
$var = file_get_contents('really_large_file');
This on the other hand won't:
$fh = fopen('really_large_file', 'r');
$aFewBytes = fread($fh, 1024);
This is the way you "manage memory" in PHP. You decide what to do. To understand how much memory each operation requires you need some understanding of what's going on behind the scenes.
Upvotes: 1
Reputation: 8701
No, we basically don't need to free memory up, as php does garbage collection automatically right after a script ends.
Upvotes: 1