-1

I have an unusual Class structure, when I'm thinking may be leading to some GC issues.

I have a Class: public class Styles

This contains a: public static class Style implements Serializable

Then I have a: public abstract class StyleDescription extends Style

Which contains a: public static final class CellStyleDescription extends StyleDescription

So, the class hierarchy of CellStyleDescription is pretty unusual.

Instances of this Class are created, serialized, then later read back in and processed. I know static classes do not handled differently by the garbage collector, but always at the end of processing, I have millions of these Classes still still in memory. Nothing in my code looks like it could be contributing to the instances not being GC'd, except this Class hierarchy.

Has anyone experienced anything similar? Or know of anything to look out for with regard to GC and serialization of static classes?

Cheers, Ro

EDIT:

What I should have pointed out is these classes are created then added to another Object (CellInfo) as member variables .... I can see the instances of this class (which is the only container of CellStyleDescription) being GC'd

4

3 回答 3

1

应用于类的关键字static只是意味着它与任何其他顶级类相同,而不是嵌套类,即使它的声明出现在另一个类中。

垃圾收集由对类的引用的生命周期控制。如果您创建大量实例并保留对这些实例的引用,那么它们将不会被垃圾收集。您的类层次结构没有任何问题。如果没有更多信息,就不可能提供更有意义的答案。

于 2013-09-04T23:17:15.310 回答
1

首先,您的层次结构并不少见。其次,在真正需要内存之前,通常不会收集对象。您应该尝试用其他对象填充内存,看看它们是否仍然存在。如果它们是,那么很可能您并没有像您想的那样释放所有对象。

可能会无意中遇到类的对象保留问题abstract。你也能做到static吗?

于 2013-09-04T23:17:21.877 回答
0

所以我发现了问题。

就像我说的那样,对象正在被序列化,然后再读回。事实证明,OubjectOutputStream 保留了对每个被写出的对象的引用。使用System.identityHashCode(obj)方法比较对象,而不是使用 Object Hashcode。

更多细节在这里:https ://www.securecoding.cert.org/confluence/display/java/SER10-J.+Avoid+memory+and+resource+leaks+during+serialization

无论哪种方式,解决方案都是定期重置 ObjectOutputStream

于 2013-09-06T08:36:44.463 回答