[英]Does all values which a static object went through take up space in memory while class is loaded?
I know that static variables are stored in a specific place in memory.我知道 static 变量存储在 memory 的特定位置。 Whenever you declare an object as static, that object will never be recycled by gc as long as that class is loaded by the classloader.. so far so good.
Whenever you declare an object as static, that object will never be recycled by gc as long as that class is loaded by the classloader.. so far so good.
But imagine that the variable that I defined as static has an internally non-static list, so even though the object itself will never be recycled, will that list be recycled normally?但是想象一下,我定义为 static 的变量有一个内部非静态列表,所以即使 object 本身永远不会被回收,该列表会正常回收吗? And even if I assign a new instance to a static object, will the old value not be recycled?
而且即使我将新实例分配给 static object,旧值不会被回收吗? Would all the values that this object went through take up space in memory?
这个 object 经历的所有值会占用 memory 中的空间吗?
The garbage collector starts its scanning from gc roots
, one such root is a static
field.垃圾收集器从
gc roots
开始扫描,其中一个根是static
字段。
So a GC
will traverse all entries that "start" from this root, for example:因此
GC
将遍历从该根“开始”的所有条目,例如:
SomeStaticInstance
|
\ /
NonStaticList
If NonStaticList
is reachable ( marking
phase of the gc phases takes care of this), it is considered alive and thus not eligible for GC
.如果
NonStaticList
是可访问的(gc 阶段的marking
阶段负责此操作),则它被认为是活动的,因此不符合GC
的条件。 If it is not reachable, it is eligible for collection.如果无法访问,则有资格收集。 Notice that if it is not reachable,
mark phase
will not even traverse anything that might have references from NonStaticList
:请注意,如果无法访问,则
mark phase
甚至不会遍历任何可能具有来自NonStaticList
的引用:
NonStaticList
|
\ /
RefHere
So RefHere
is not going to be scanned.所以
RefHere
不会被扫描。 It might get scanned if it has references from somewhere else to it, but not from NonStaticList
.如果它有来自其他地方的引用,但不是来自
NonStaticList
,它可能会被扫描。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.