Question

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?

Était-ce utile?

La solution

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

Autres conseils

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.

No, there is no relation. Keep in mind that if the GC eventually cleaned everything properly, you don't suffer from a case of memory leak; it's the way GC works. You shouldn't be worried about late GC's, and if you are, you just need to tune the JVM arguments.

Transience will not effect garbage collection.

  • Perhaps look for objects being used in a tight loop, where the objects have references to each other this could slow garbage collection down.

  • Perhaps explore using Java's weak references.

  • Perhaps look at tuning the garbage collection settings. ConcurrentSweepGC can help.

  • Perhaps just look at allocating more stack and heap space.

"Transient objects that can be garbage collected" do exist in Java, use WeakReference for objects that should be collected as soon as gc runs and SoftReference for something that is better to keep but should be discarded if the memory runs low.

The transient keyword has no effect on memory management.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top