简体   繁体   English

Java:是否收集了临时对象垃圾?

[英]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. 当内存使用量达到峰值时,GC计数会更多,并且会垃圾回收对象。

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? 但是,过渡性与垃圾回收有关系吗?

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). 基本上,我认为您需要在其他地方寻找泄漏(如果确实存在泄漏)。

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. transient关键字指示不应将其序列化,因此,如果有任何含义,则表示反序列化的对象比原先的对象要小。

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. 在这种情况下,您需要通过设置-Xmx-mx命令行选项来增加最大内存。

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; 请记住,如果GC最终正确地清理了所有内容,则不会出现内存泄漏的情况。 it's the way GC works. 这就是GC的工作方式。 You shouldn't be worried about late GC's, and if you are, you just need to tune the JVM arguments. 您不必担心后期的GC,如果您愿意,只需调整JVM参数即可。

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. 也许探索使用Java的弱引用。

  • Perhaps look at tuning the garbage collection settings. 也许看看调整垃圾收集设置。 ConcurrentSweepGC can help. ConcurrentSweepGC可以提供帮助。

  • 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. Java中确实存在“可以被垃圾收集的瞬态对象”,将WeakReference用于应在gc运行时立即收集的对象,将SoftReference用于可以更好地保留的对象,但如果内存不足,则应将其丢弃。

The transient keyword has no effect on memory management. transient关键字对内存管理没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM