I have the following code:
@JmsListener(...)
Public void messageListener(TextMessage message){
System.out.println(message.getText());
//Writing string to a file...and then
//clear it from heap space
message.clearProperties();
//Once removed from heap read from file and process string
processString();
}
Does this remove the String
from heap space. Actually, the message I pick up from the MQ maybe 20MB so I want to write it to a file and then clear it from my heap space. Would this work? Please let me know how I can phrase the question better.
The JavaDoc for javax.jms.Message.clearProperties()
states:
Clears a message's properties.
The message's header fields and body are not cleared.
If the String
you care about is retrieved using javax.jms.Message.getText()
then that means the String
is part of the body of the message which clearProperties()
doesn't even touch.
In any event, the only thing that frees heap space is the garbage collector and you have no direct control over that. The best you can do is make sure the Object
you are interested in has no references. However, in this situation you're working with an API (i.e. JMS) whose implementation is not clear so even doing something like setting message = null
might not be sufficient for GC to collect it since the underlying implementation may still have references to it.
Using javax.jms.Message.clearBody()
would seem more appropriate for you use-case, but even then the JMS API makes no guarantee that the underlying message data would be free for garbage collection. The exact behavior would depend entirely on the JMS implementation you were using.