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;
}
}
What memory leak? Java has garbage collection. If an object isn't
reachable, its memory is reclaimed.
It's impossible to know for sure what's going on in your code. You
posted precious little of it. If the "CashInTable" field of each class
is to refer to the same object, that's probably a serious design
problem. But only because of data coherency and code maintenance
issues. It doesn't lead to memory leaks.
(In fact there are others like me who have a strict definition of
"memory leak" that precludes even the possibility of a leak in a GC
system. Your code can "pack-rat" data � that is, retain references to
objects that are actually no longer needed � but a true memory leak,
where some block of memory remains allocated but completely outside of
the program's reach, just isn't possible).
Pete
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
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/>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
> 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.
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.
> return null;
> }
> }
--
Lew
>
>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
--
Roedy Green Canadian Mind Products
http://mindprod.com
Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
Sometimes if you need a mapping but don't want to bother cleaning it up
after the Mapped Object becomes obsolete the WeakHashMap might help you.
Though that depends greatly on use...
Christian
Perhaps he refers to the 'Implementation note' in that class's docs. It
warns that if the value refers to the key, the entry won't be discarded,
even though no-one else is using the key. It's as if the key and value
are keeping each other alive, even though the GC ought to cope with such
loops. But it's the map that's keeping both alive: it holds an entry,
which holds the value, which holds the key, which prevents the map from
discarding the entry.
It's not that the WeakHashMap is causing a 'leak' - a plain HashMap
would surely be at least as bad - but that the user's expectation of WHM
is not met because he hasn't noticed the loop that stops it doing its job.
> A WeakHashMap uses WeakReferences as entries, which allows the GC to
> garbage collect the values referenced.
Strictly speaking, it's only the keys that are held weakly. When they
are discarded, their corresponding entries are removed, which might
result in the values being GCed.
--
ss at comp dot lancs dot ac dot uk