Showing posts with label Memory Leaks. Show all posts
Showing posts with label Memory Leaks. Show all posts

Sunday, January 16, 2011

Memory Leaks in JavaScript and Solutions - Tutorial 1

Memory leaks in java script
----------------------------
In the first of these tutorials, let us highlight 3 major areas : Circular References , Anonymous Inner Functions, Closure.

 Problem 1 : Circular references.

So, we have an object, and we have a property on that object. Now we assign that property back to the object itself. Bam - memory leak.
A more realistic example is perhaps where we have object 1, then we have a property in that object which points to object 2. Now, add a property to object 2, which points back to object 1::



for (var i = 0; i < 10000; i  ) {
    var myDiv = document.createElement('div');
    var myDiv2 = document.createElement('div');


    myDiv.expandoProperty = myDiv2;
    myDiv2.expandoProperty = myDiv;
}



Solution
-----------

Nullify your properties.
When you are done with your crazy web of circular property references, simply nullify the properties:
 myDiv.expandoProperty = null;
 myDiv2.expandoProperty = null;

Read More on the next two problems

Thursday, January 13, 2011

Java : Best Practices for avoiding memory leaks in Java

Hope you find this post useful with the TOP-20 memory leak prevention steps.

1.Use logging frameworks like Log4J which uses I/O buffers instead of System.out.println.
2.Set the initial capacity of a collection.Empty vector has array size 10.
3. Collection classes, such as hash tables and vectors, are common places to find the cause of a memory leak.
4.Use ArrayLists, HashMap instead of Vector,Hashtable etc wherever possible.
5. Unregister when the class is no longer needed. Otherwise it will exist until the application close.
6.Use Thread pooling and database connection pooling techniques wherever possible.
7.In case of I/O operations use buffering when writing to and reading from
files and/or streams.Avoid reader/writers use streams instead of this.
8. Integrate your java application with JProfiler so that you can check which class or method is taking more memory.
9. If you are application using database ensure that tables are not containing unwanted data [historical data].
   otherwise it will load every time when application starts and collection classes’ size will increase.
10. Application design is also an important factor for memory leak. If possible do changes in design level.
11. Use mutable StringBuffer/StringBuilder classes instead of immutable String objects.
12.Remove references  to the short-lived objects from long-lived objects like Java collections.
13.Create objects before entering the loop and avoid creating new objects for each iteration.

14.Reduce calls to Date, Calendar, etc related classes.
15.Use tools like JProfiler,JProbe,OptimizeIt to find memory leaks in your java application.


When working with Swings,windows and Threads
-----------------------------------------------------------------

Java talks to the underlying GUI which is typically written in C++ which does not have automatic garbage collection.
If you fail to use dispose method properly, Java won’t run out of RAM, but the GUI will.
16. Failing to use dispose method create true memory leaks.
17. Threads are huge objects. Make sure you nullify pointers to them after you no longer need them.
   Make sure you properly terminate threads or you will fill up RAM with moribund Threads.
18. Don’t create Threads until you are ready to start them.
19. In Swing make sure you use JFrame. setDefaultCloseOperation ( JFrame. DISPOSE_ON_CLOSE );
    instead of the default JFrame.setDefaultCloseOperation ( JFrame.HIDE_ON_CLOSE );
otherwise, when a user closes a window, the frames are just hidden. You don’t recover the resources.
20. Use JFrame. setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE ); when you have a WindowListener. windowClosing method.

21. To permanently close a frame :
frame.setVisible( false );
frame.dispose();
frame = null;

subversion video