Re: Preventing Memory Leak when using HashMap
Krist wrote:
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.
return null;
}
}
--
Lew