Solved

Memory Leakage , Out of Memory

Posted on 2007-11-21
17
240 Views
Last Modified: 2008-02-01
Hi,
  There is memory leakage in our application as it resuts into outofmemory exception after week time.

I want to
1.some practical examples to remove unwanted objects referece?
2.Some code examples.
3.IF this is my code
Public MySvc{
Map editors_=null;
private void myMethod(){

Map newEditors = Dao.getEditors();


editors = newEditors;

}

here newEditors will get GC or not? THis Svc class always active.

3.Some tools to find this memory leakage or out of memory.

Most helpful will be some code to understand me that here object will not get GC and here it will get GC.




}
0
Comment
Question by:Manish
  • 5
  • 4
  • 4
  • +2
17 Comments
 
LVL 9

Expert Comment

by:ysnky
Comment Utility
gc will not be called for an object which is refered by others. be carefull about connections (db, socket etc...) are closed after used.

http://jb2works.com/memoryleak/index.html
0
 
LVL 13

Assisted Solution

by:Bart_Cr
Bart_Cr earned 60 total points
Comment Utility
The only thing possibly eligible for GC when the method exits is the previous value (Map) referenced by 'editor'.

Typical causes:

Objects stored in Collection and never removed
Objects stored in ThreadLocal and never removed
Objects stored in HttpSession and never removed
...

Simply make sure you clear references (instance level) whenever possible. Remove them from collections, ...

For tools, NetBeans has got a profiler out of the box. You can also run the java process using -Xrunhprof. You can open the generated log file in HP JMeter.
To get an insight in garbage collection, download visualgc.

0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 20 total points
Comment Utility
> here newEditors will get GC or not? THis Svc class always active.
 
NO. You need to dereference it. or wrap this variable using WeakReference
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Your method:

private void myMethod()
{
   Map newEditors = Dao.getEditors();
   editors = newEditors;
}

After return from this method, 'newEditors' is certainly lost and the reference is virtually null.
But .... which other references still 'look at' that object.
Further: the worst problem with objects not garbage collected is those that you
'give' to handlers like those that you add as listeners.
Take care to .remove() everything you once .add()  added.

;JOOP!
0
 
LVL 11

Author Comment

by:Manish
Comment Utility
Still I am not clear abt this..
private void myMethod(){

Map newEditors = Dao.getEditors();
editors = newEditors;
//Should I write this or not?
newEditors=null;
//If I dont write code..newEditors=null ,is there memory leakage?
}
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Map newEditors               is local to the method, so it will go after return.
newEditors=null;             is useless.

;JOOP!
0
 
LVL 11

Author Comment

by:Manish
Comment Utility
So,
editors which is class level does not keep refrence to method level variable. in this case newEditors.
editors variable is always available when we locate this class.
Am I correct?


0
 
LVL 9

Expert Comment

by:ysnky
Comment Utility
let's analys your code;

private Map = editors;
private void myMethod(){

Map newEditors = Dao.getEditors();  // memory allocates with Dao.getEditors() and reference by newEditors
editors = newEditors; // the same memory block will be referance by editors too
newEditors=null; //local referance null but the the memory block is still refered by editors so it will be stay in memory. so newEditors=null is useless.
}


>>editors which is class level does not keep refrence to method level variable. in this case newEditors.
infact it refers to Dao.getEditors()
>>editors variable is always available when we locate this class.
yes.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Author Comment

by:Manish
Comment Utility
Is anybody make this class such a way there will be  memory leak?
So that I can understand it.

0
 
LVL 13

Assisted Solution

by:Bart_Cr
Bart_Cr earned 60 total points
Comment Utility
It's nearly impossible to make a single object a cause for a "memory leak". It all depends on how and when the class is used:
- which objects does the object store in its fields
- what does it return from instance methods
- what do other objects do with regard to the objects returned from the instance methods

An object is only eligible for garbage collection if it is no longer reference from a "root node". So an entire object graph (with 1000's of objects) can be collect at once as soon as the link to the root node is cut.

So, to take your (slightly changed) example:

Map editors =oldEditors;

private void myMethod(){
    Map newEditors = Dao.getEditors();
    editors = newEditors;
}

After myMethod exits the only object possible eligible for garbage collection is "oldEditors". The value returned from Dao.getEditors() is can't be collected unless no reference to the object containing the field is kept in the calling code:

void myCalled() {
     CalledObject obj = new CalledObject();
     obj.myMethod();
}

Here "obj" only exists in the scope of the method and can be collected. Resulting in editors to be collected, if and only if DAO doesn't keep a reference somewhere:

public class DAO {
    private static Map editorCache;

    public Map editors getEditors() {
        if (editorCache == null) {
            editorCache = getEditorsTrueSomeSmartCode();
        }
        return editorCache;
    }
}

This makes that although CalledObject can be collected, the editors still won't be collected as they are cached in the DAO.

To make a long post short: It's nearly impossible to state when an object will be collected and when not. The entire stack of called methods/classes needs to be analyzed to make a statement about the reachability of objects.
0
 
LVL 9

Accepted Solution

by:
ysnky earned 45 total points
Comment Utility
there is no memory leak in your code and making it is not so easy.

have a look at this;
http://www.adtmag.com/java/articleold.aspx?id=165

you can use these tools to detect memory leaks;
OptimizeIt, JProbe, JInsight
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Do you want to have a memory leak or do you want to get rid of one?

;JOOP!
0
 
LVL 11

Author Comment

by:Manish
Comment Utility
There is memory leak in our application. I want to find that point.
I thing we have to use tools like vsnky suggessted OptimizeIt, JProbe, JInsight.
I dont know which one will be good...
if Anybody tell me best way to find that point..then i will follow that way.


0
 
LVL 9

Assisted Solution

by:ysnky
ysnky earned 45 total points
Comment Utility
0
 
LVL 13

Assisted Solution

by:Bart_Cr
Bart_Cr earned 60 total points
Comment Utility
JProbe is good, but expensive. A good alternative is to start with either NetBeans profiler, or use the built-in profiler in the JVM (-Xrunhprof). The resulting file of runhprof can be loaded with HPJMeter afterward and analyzed.
Another thing to use might be visualgc. It will give you a better insight on how the heap is divided and performs.
0
 
LVL 13

Expert Comment

by:Bart_Cr
Comment Utility
Another thing you might want to take a look at is JConsole. Also freely available with your JDK.

http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
0
 
LVL 11

Author Comment

by:Manish
Comment Utility
I am using JProfiler but still not getting to catch it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert ArrayList of custom objects to JSON 4 53
countHi2 challenge 7 44
mapAB Challlenge 35 84
HashMap Vs TreeMap 12 48
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now