Memory problem in php


i want to know about how the php handle memory allocation, if i use the oops concept how the allocated object memory will free ,it will automatically , can any one tell about this. how we use effective memory utilization in php.

PHP doesn’t allow you to directly access the memory. You can monitor memory consumption during the script and use unset to manually clear variables you know you aren’t going to use, though PHP’s garbage collection will wipe these eventually anyway. Your question does raise the red light of “are you sure PHP is the language to use for this?” because memory management is typically the last consideration of most PHP scripts, and some of the most popular are veritable pigs with memory (here’s looking at you Magento).

This has worked for me in the past:


ew. Good way to have your server overload.

Might be worth reading:

Most of my scripts use a recursion that uses that same variables over and over again. Most operations done in PHP aren’t dealing with objects large enough to have a significant impact on your memory usage. There is a garbage collection function as of 5.3 something, though, which i have yet to find the need to use yet.

Ok, fair point - maybe ‘-1’ is a bit drastic, but it could be used with a specific value:


if i use 50mb in limit, i can use only 50mb if my application want to user more than that what will happen, i want to make sure if i didnt unset the unused variable,mysql free result it will automatically clear when the script end ,my understanding is correct ?. else it will raise memory problem.


Sets the value of the given configuration option. The configuration option will keep this new value during the script’s execution, and will be restored at the script’s ending.
quoted from so you don’t need to worry about unsetting.

ini_set(“memory_limit”,“50m”); sets an upper limit of 50mb - you need to know roughly what your upper limit is for this to work satisfactorily.

ok thanks for your suggestion. i dont want to specify by setting,whether php manage automatic garabage collection , while doing code i have to do memory release ?

What exactly are you doing that would require more than 50 MB of memory usage? o.O

Large image manipulation - that’s all I’ve got. That or he’s running Magento.

just i want to know about it .