• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection question

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic