Solved

Memory Leakage , Out of Memory

Posted on 2007-11-21
17
261 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
ID: 20327617
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 Cremers
Bart Cremers earned 60 total points
ID: 20327663
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
ID: 20328039
> 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 24

Expert Comment

by:sciuriware
ID: 20328260
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
ID: 20332583
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
ID: 20332879
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
ID: 20332897
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
ID: 20332956
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
 
LVL 11

Author Comment

by:Manish
ID: 20333641
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 Cremers
Bart Cremers earned 60 total points
ID: 20333729
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
ID: 20333751
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
ID: 20334243
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
ID: 20337296
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
ID: 20337525
0
 
LVL 13

Assisted Solution

by:Bart Cremers
Bart Cremers earned 60 total points
ID: 20337646
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 Cremers
ID: 20337654
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
ID: 20380265
I am using JProfiler but still not getting to catch it.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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