By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

memory leakage

pradeepjain
100+
P: 563
Hii guys,
can any one explain me what is memory leakage .And how to prevent is in code.And any way to check memory leakage.




thanks,
Pradeep
Oct 3 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 310
I don't think you ever have to be concerned about memory leaks with PHP.

Memory leaks occur when your program reserves memory to dynamically create an object in your program, and then all of the references to that object are destroyed without formally releasing the memory again. The operating system, e.g. Windows, will continue to see this memory as being reserved and will not be able to use it again until you reboot your computer. Since all of the references to the object in your program have been destroyed, your program can no longer release this memory.

Dynamic objects are created by allocating memory on what is called the heap, and the variable in your program is just a reference to that object, it just points to it. If you then break the connection between that reference and the object's memory that it is pointing to, without releasing (unreserving) the memory, you get a memory leak. If this keeps happening again and again in your program, at some point your operating system runs out of available memory because everything is reserved for these no longer existing objects.

Here an example:
Expand|Select|Wrap|Line Numbers
  1. // something maybe from C++
  2. MyClass myObject = new MyClass();
  3. ...
  4. // reuse the reference myObject for another object
  5. myObject = new MyClass();
  6.  
Its been a long time since I programmed C++, so maybe I don't get it all right. But the idea is that I created a reference myObject that points to a new object of my class MyClass, and this allocates memory on the heap. Later, I forget to "destroy" (tell the code to unreserve) the object, and then reuse the reference myObject in creating a new object and reserving more memory. The original memory is now reserved and unusable for anyone.

This is what a memory leak is basically all about.

However, unless I am wrong, this is nothing you need to worry about with PHP. When a script is finished running, I believe the PHP interpreter releases all memory and all system resources that the script has used, it does this for you automatically.
Oct 3 '08 #2

Atli
Expert 5K+
P: 5,058
Yea this is not something you need to worry about with PHP.

Memory leaks are usually only found in programs that have direct access to the memory, like C/C++ for example.

In PHP, as well as most modern languages (like Java and C#), the interpreter will manage this for you automatically. It will allocate memory for you variables for you and release them when they are no longer needed.

But...
If you are creating a resource intense application in PHP, you may need to unset variables to make sure you are not using to much memory.

Like say, if your application is processing a lot of images you may want to unset the variables when they are no longer needed to free up memory for the rest of the process.
Processing ten 1MB images one after the other will build up to 10MB of memory used unless you tell PHP to unset the variables when you are done with each one.
Oct 4 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.