Preventing Memory Leak when using HashMap

K

Krist

Hi all,

Two classes below pass HashMap to each other, I want to avoid the
memory leak, other than using HashMap.remove(key), how to avoid memory
leak in my code below ?

FormInv class get return value from a lookup on class CashLookUp,
these are simplified code.

Thank you for your help,
Krist


public class FormInv extends PageController {
private HashMap CashInTable;
public void CashIn_returnAction(ReturnEvent returnEvent) {
String vCode = null ;
String vNo = null ;

if (returnEvent.getReturnValue()!=null){
this.CashInTable =(HashMap)returnEvent.getReturnValue();
vCode = (String)this.CashInTable.get("vDocCode");
vNo = (String)this.CashInTable.get("vDocNo");

// Is this the only way to avoid memory leak with this HashMap ?
// CashInTable.remove("vDocCode");
// CashInTable.remove("vDocNo");
}
}
}

public class CashLookUp {
private HashMap CashInTable;
public String selectButton_action() {
JUCtrlValueBindingRef
tabelCheck=(JUCtrlValueBindingRef)this.getCashInLov_Table().getRowData();
String docCode =
(String)tabelCheck.getRow().getAttribute("DocCode");
String docNo =
(String)tabelCheck.getRow().getAttribute("DocNo");
CashInTable= new HashMap();
CashInTable.put("vDocCode",docCode);

CashInTable.put("vDocNo",docNo);

AdfFacesContext.getCurrentInstance().returnFromDialog(CashInTable,null);
return null;
}
}
 
K

Krist

Hi Sir,

CashInTable is HashMap object created in CashLookUp class, this is a
JSF backing bean. part of JSF framework.

This code
AdfFacesContext.getCurrentInstance().returnFromDialog(CashInTable,null);
In that class will pass the the HashMap object into the calling page
(then its backing bean take action)

Than the backing bean of the calling page (FormInv) take action.
this.CashInTable =(HashMap)returnEvent.getReturnValue();

So, both class has local variable with (a coincidence) same name.

This will not be GCed, isn't it ? I read anywhere that unremoved
HashMap is one of the factor of Java memory leak.

Thanks,
Krist
 
J

John B. Matthews

Krist said:
CashInTable is HashMap object created in CashLookUp class, this is a
JSF backing bean. part of JSF framework.

This code
AdfFacesContext.getCurrentInstance().returnFromDialog(CashInTable,null);
In that class will pass the the HashMap object into the calling page
(then its backing bean take action)

Than the backing bean of the calling page (FormInv) take action.
this.CashInTable =(HashMap)returnEvent.getReturnValue();

So, both class has local variable with (a coincidence) same name.

This will not be GCed, isn't it ? I read anywhere that unremoved
HashMap is one of the factor of Java memory leak.

I can't tell if your HashMap is retaining references inappropriately, as
Pete outlined above, but this article discusses the problem:

<http://www.ibm.com/developerworks/java/library/j-jtp11225/>
 
M

markspace

Krist said:
So, both class has local variable with (a coincidence) same name.

This will not be GCed, isn't it ? I read anywhere that unremoved
HashMap is one of the factor of Java memory leak.


If the HashMap becomes unreachable, it will be garbage collected. A
HashMap only "leaks" when you have a reference to the HashMap you want
to keep, and you want to remove a reference inside the hash map. In
fact, I believe this problem only really occurs when you have a
WeakHashMap, which can accidentally hold on to it's own references.

If you are using a local variable, then you are fine. The hash map will
be eligible for garbage collection as soon as the local variable goes
out of scope.
 
L

Lew

Krist said:
Two classes below pass HashMap to each other, I want to avoid the
memory leak, other than using HashMap.remove(key), how to avoid memory
leak in my code below ?

Others answered your primary question, so I have just incidental remarks.
public class FormInv extends PageController {
private HashMap CashInTable;

Variables should be named with an initial lower-case letter by Java convention.
public void CashIn_returnAction(ReturnEvent returnEvent) {

Methods should be named with an initial lower-case letter and contain no
underscores by Java convention.
String vCode = null ;

Variables should be declared in the narrowest scope to which they apply. This
is important to prevent memory "leaks" in Java.

The initialization to 'null' here is useless and should not be done.
String vNo = null ;

if (returnEvent.getReturnValue()!=null){
this.CashInTable =(HashMap)returnEvent.getReturnValue();

The only chance of a "leak" here is in the code you chose not to share.
vCode = (String)this.CashInTable.get("vDocCode");
vNo = (String)this.CashInTable.get("vDocNo");

// Is this the only way to avoid memory leak with this HashMap ?
// CashInTable.remove("vDocCode");
// CashInTable.remove("vDocNo");
}
}
}

public class CashLookUp {
private HashMap CashInTable;
public String selectButton_action() {
JUCtrlValueBindingRef
tabelCheck=(JUCtrlValueBindingRef)this.getCashInLov_Table().getRowData();
String docCode =
(String)tabelCheck.getRow().getAttribute("DocCode");
String docNo =
(String)tabelCheck.getRow().getAttribute("DocNo");
CashInTable= new HashMap();
CashInTable.put("vDocCode",docCode);

CashInTable.put("vDocNo",docNo);

AdfFacesContext.getCurrentInstance().returnFromDialog(CashInTable,null);

The only chance of a "leak" here is in the code you chose not to share.
 
R

Roedy Green

This will not be GCed, isn't it ? I read anywhere that unremoved
HashMap is one of the factor of Java memory leak.

A leak would be a bug in the JVM. I don't know of any outstanding
such bugs, but certainly using an ordinary HashMap will not cause one.

It is hard to understand what you are asking. All I can do is to point
you to some docs to help you understand how memory management works.

see http://mindprod.com/jgloss/garbagecollection.html
http://mindprod.com/jgloss/packratting.html
 
D

Daniel Pitts

If the HashMap becomes unreachable, it will be garbage collected. A
HashMap only "leaks" when you have a reference to the HashMap you want
to keep, and you want to remove a reference inside the hash map. In
fact, I believe this problem only really occurs when you have a
WeakHashMap, which can accidentally hold on to it's own references.
That makes no sense...
A WeakHashMap uses WeakReferences as entries, which allows the GC to
garbage collect the values referenced. Any object is free to hold a
reference to itself without affecting GC. The only requirement for GC
eligibility is that the object itself isn't accessible through any stack
(directly or indirectly). Reference count is not at all a test.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top