Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Are transient objects garbage collected?

My program showed trends of memory leak. When the memory usage reached the peak, the GC count was more and the objects were garbage collected.

We found that a class which was the reason for the memory leak trend.

But I would like to check why the class was actually garbage collected and when I explored the class, there was only one transient object in the class.

Transient objects are objects that are not serialized. But does the transient nature have anything to do with garbage collection?

like image 892
user1979776 Avatar asked Dec 21 '22 11:12

user1979776


2 Answers

There's no such thing as a transient object. There are transient fields, which are ignored during serialization - but that has no effect on garbage collection.

Basically, I think you need to look elsewhere for your leak (if indeed you even have a leak).

like image 88
Jon Skeet Avatar answered Jan 12 '23 06:01

Jon Skeet


does transient nature have to do anything with garbage collected?

No, nothing.

The transient keyword indicates it should not be Serialized so if anything it will mean Deserialized objects are smaller than they would otherwise be.

We found that a class which was the reason for the memory leak trend.

You will have a memory leak because you are keeping such a object in a collection when you don't need it. You have to make sure that objects you retain this way are removed when you don't need them.


Just because you are retaining data, doesn't mean you have a leak. You could be needing that data so you need more memory than you expected. In this case you need to increase the maximum memory by setting the -Xmx or -mx command lines options.

like image 43
Peter Lawrey Avatar answered Jan 12 '23 06:01

Peter Lawrey