Aptitude (12) ASP.NET (2) Automata (4) Browser (1) C (5) C# (1) C++ (10) Code (3) CSS (1) Data Structure (1) DATABASE (3) HTML (1) java (43) JSP (1) math (1) MySql (8) other (6) php (3) Servlet (3)

Saturday, 9 March 2013

Memory Leakage

Memory  Leakage:
Arising due to having lost the pointer to some memory that is allocated on heap.in this case the memory remains allocated and can't be freed because it's base address is lost.it leads to substantial wastage of the free storage on the heap.


int *p=new int[1000];
----
----
delete []p;

4bytes
------------
4000bytes

if delete p
only first 4 bytes freed which results in memory leakage of 3996 bytes

Note:Also it is very important to note that whenever we have allocated memory for more than one element i.e using subscript notation [size],it becomes necessary to de-allocate the memory by using the subscript notation [] in the delete operator as shown above.if this is not done and a delete  statement without a pair of
 [ ] is used,then only the first element whose address is contained in the pointer is deallocated resulting in the problem of memory leakage.

Memory leak
Memory leaks can be really annoying. The following list describes some scenarios that result in memory leaks.
  • Reassignment I'll use an example to explain reassignment.
    char *memoryArea = malloc(10);
    char *newArea = malloc(10);

    This assigns values to the memory locations shown in Figure 4 below.


    Figure 4. Memory locations


    memoryArea and newArea have been allocated 10 bytes each and their respective contents are shown in Figure 4. If somebody executes the statement shown below (pointer reassignment )…
    memoryArea = newArea; 

    then it will surely take you into tough times in the later stages of this module development.In the code statement above, the developer has assigned the memoryArea pointer to the newArea pointer. As a result, the memory location to which memoryArea was pointing to earlier becomes an orphan, as shown in Figure 5 below. It cannot be freed, as there is no reference to this location. This will result in a memory leak of 10 bytes.


    Figure 5. Memory leak


    Before assigning the pointers, make sure memory locations are not becoming orphaned.
  • Freeing the parent block first Suppose there is a pointer memoryArea pointing to a memory location of 10 bytes. The third byte of this memory location further points to some other dynamically allocated memory location of 10 bytes, as shown in Figure 6.


    Figure 6. Dynamically allocated memory


    free(memoryArea)

    If memoryArea is freed by making a call to free, then as a result the newArea pointer also will become invalid. The memory location to which newArea was pointing cannot be freed, as there is no pointer left pointing to that location. In other words, the memory location pointed by newArea becomes an orphan and results in memory leak.
    Whenever freeing the structured element, which in turn contains the pointer to dynamically allocated memory location, first traverse to the child memory location (newArea in the example) and start freeing from there, traversing back to the parent node.
    The correct implementation here will be:
    free( memoryArea->newArea);
    free(memoryArea);
  • Improper handling of return values At time, some functions return the reference to dynamically allocated memory. It becomes the responsibility of the calling function to keep track of this memory location and handle it properly.
    char *func ( )
    {
    return malloc(20); // make sure to memset this location to ‘\0’…
    }

    void callingFunc ( )
    {
    func ( ); // Problem lies here
    }

    In the example above, the call to the func() function inside the callingFunc() function is not handling the return address of the memory location. As a result, the 20 byte block allocated by the func() function is lost and results in a memory leak.


No comments:

Post a Comment