memory leakage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    memory leakage

    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
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    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:
    Code:
    // something maybe from C++
    MyClass myObject = new MyClass();
    ...
    // reuse the reference myObject for another object
    myObject = new MyClass();
    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.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

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

      Comment

      Working...