Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection question

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic