It's not a secret anymore!*
The moose likes Java in General and the fly likes Garbage Collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage Collection question" Watch "Garbage Collection question" New topic
Author

Garbage Collection question

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi,
I have a JMS service that acts as the server. Once after delegating the requests to the client, the server still holds the objects (result sets needed by clients) in the memory.
The response object is an HashMap object which in turn holds the actual result set objects of calculation. I made the response object as null and called GC but still the objects are not garbage collected as the memory heap shows nearly 700 MB of increase. Can you help me which object should be made as null for GC. Below is a snippet on how the result is given back to the client.

The resultsList object in the above code has around ~97,000 objecs of the DAO with each object having around 10 variables with String as the value.

Now the map is got by the below code snippet and returned back to the client. Please note that i have made the result value as null.

Please let me know how to mark all DAO objects present in the List as null & enable for GC.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

1) remove all references to the List. The List will then be eligible for GC, and because of that so will all of its elements that have no other references.
2) clear() the List.
3) remove all references to the HashMap. This will make the HashMap eligible for GC; because of this, the List will become eligible for GC (provided there are no other references), and you'll get to point 1.

Actually, unless I'm mistaken, you don't need to do anything. When the processMessage method ends both the result (the HashMap) and replyMessage (which uses result) variables go out of scope. Now the only reference will be in replyProducer. Once that's done with it, there will be no more references and the ObjectMessage, HashMap and List will be eligible for GC.

Also note that the call to System.gc() won't do anything there because you still have a reference inside the ObjectMessage. Besides, System.gc() is not guaranteed to run when you want it to. Just remove the last two lines of the method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Is the reference returnMap declared at class level; it does not seem to be a local variable of the function processObject().Hence even though the process is ending by returning value to result and you are making the result null the returnMap is still pointing to it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You're right, I missed that. It's a field alright, either static or not. So turn it into a local variable, unless there's a valid reason to keep it as a field.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Rob and Ashutosh,
Thanks for the replies. And the returnMap is a local variable only. I was able to modify the code based on the suggestions.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection question
 
Similar Threads
GC question..........
MDB: Message id is null Error
From Array of Objects to HashMap
Make an interface serializable
nested logic:iterate