Java中的循环引用 [英] Circular References in Java

查看:95
本文介绍了Java中的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个以复杂,循环,时尚相互引用的类实例的聚合:垃圾收集器可能无法释放这些对象吗?

我隐约记得这在过去是JVM中的一个问题,但我认为这在几年前就已经解决了。然而,jhat中的一些调查显示,循环引用是我现在面临的内存泄漏的原因。



注意:我一直处于JVM能够解决循环引用并从内存中释放这些垃圾岛的印象。然而,我提出这个问题只是为了看看是否有人发现任何异常。

实施会遇到循环引用的问题。 Wikipedia对不同的GC算法有很好的文章。如果您真的想了解更多信息,请尝试(Amazon)垃圾收集:自动动态内存管理算法。从1.2开始,Java已经有了一个很好的垃圾回收器,并且在1.5和Java 6中也是非常好的垃圾回收器。

改进GC的难点在于减少停顿和开销,而不是基本的东西像循环引用。


Given an aggregation of class instances which refer to each other in a complex, circular, fashion: is it possible that the garbage collector may not be able to free these objects?

I vaguely recall this being an issue in the JVM in the past, but I thought this was resolved years ago. yet, some investigation in jhat has revealed a circular reference being the reason for a memory leak that I am now faced with.

Note: I have always been under the impression that the JVM was capable of resolving circular references and freeing such "islands of garbage" from memory. However, I am posing this question just to see if anyone has found any exceptions.

解决方案

Only a very naive implementation would have a problem with circular references. Wikipedia has a good article on the different GC algorithms. If you really want to learn more, try (Amazon) Garbage Collection: Algorithms for Automatic Dynamic Memory Management . Java has had a good garbage collector since 1.2 and an exceptionally good one in 1.5 and Java 6.

The hard part for improving GC is reducing pauses and overhead, not basic things like circular reference.

这篇关于Java中的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆