Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Memory Leakage , Out of Memory

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
Manish
Asked:
Manish
  • 5
  • 4
  • 4
  • +2
6 Solutions
 
ysnkyCommented:
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
 
Bart CremersJava ArchitectCommented:
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
 
Ajay-SinghCommented:
> 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sciuriwareCommented:
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
 
ManishLeadAuthor Commented:
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
 
sciuriwareCommented:
Map newEditors               is local to the method, so it will go after return.
newEditors=null;             is useless.

;JOOP!
0
 
ManishLeadAuthor Commented:
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
 
ysnkyCommented:
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
 
ManishLeadAuthor Commented:
Is anybody make this class such a way there will be  memory leak?
So that I can understand it.

0
 
Bart CremersJava ArchitectCommented:
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
 
ysnkyCommented:
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
 
sciuriwareCommented:
Do you want to have a memory leak or do you want to get rid of one?

;JOOP!
0
 
ManishLeadAuthor Commented:
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
 
ysnkyCommented:
0
 
Bart CremersJava ArchitectCommented:
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
 
Bart CremersJava ArchitectCommented:
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
 
ManishLeadAuthor Commented:
I am using JProfiler but still not getting to catch it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now