什么触发了Java垃圾收集器 [英] What triggers the java Garbage Collector

查看:102
本文介绍了什么触发了Java垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我得到一个对象在没有更多实时引用的情况下有资格进行垃圾回收,但是如果它包含活动对象的引用呢?



可以说我有一个节点集合,可以再次引用更多节点

 列表
1 - >节点a - >节点b
2 - >节点c - >节点d
3 - >节点d - >节点c
4 - >节点e
5

现在,如果我删除列表,节点cd和e应该是垃圾集。节点e没有更多的引用,并且节点c和d有循环引用。



但是节点a呢?它会被垃圾回收吗?



无论节点b是否具有外部实时引用,它会不同吗?如果节点b从一个不同的地方引用它,是否会使节点停留在内存中?解析方案

引用的根集(当前局部变量,静态引用,堆栈帧的操作数堆栈),这被认为是实时的。任何无法从此根引用集合获得的内容都有资格进行垃圾回收。



节点a没有任何引用指向它。因此,即使它指的是一个活动对象,它也有资格使用gc。由于节点b有实时参考,它不会被gc'ed。


I am a little confused to how garbage collection in Java works.

I get that an object becomes eligible for garbage collection when there are no more live references to it, but what if it has references to live objects?

Lets say i have a collection of nodes that again references more nodes

List
1 -> Node a -> Node b
2 -> Node c -> Node d
3 -> Node d -> Node c
4 -> Node e
5

Now if i delete the list, node c d and e should be garbage collected. Node e has no more references to it, and node c and d have cyclical references.

But what about Node a? Will it be garbage collected?

Will it be different whether or not node b has outside live references? Say if node b has a reference to it from a different place, will that make node a stay in memory?

解决方案

There is a root set of references (current local variables, static references, operand stacks of stack frames), that is considered live. Anything that is not reachable from this root set of references is eligible for garbage collection.

The node a is not having any reference pointing to it. So it is eligible for gc even if it is referring to a live object. Since node b is having live reference, it wont get gc'ed.

这篇关于什么触发了Java垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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