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.
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.
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.