Link to home
Start Free TrialLog in
Avatar of Manish
ManishFlag for India

asked on

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.




}
Avatar of ysnky
ysnky
Flag of Türkiye image

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
SOLUTION
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sciuriware
sciuriware

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!
Avatar of Manish

ASKER

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

;JOOP!
Avatar of Manish

ASKER

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?


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.
Avatar of Manish

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you want to have a memory leak or do you want to get rid of one?

;JOOP!
Avatar of Manish

ASKER

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.


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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/
Avatar of Manish

ASKER

I am using JProfiler but still not getting to catch it.