简体   繁体   English

static object 经历的所有值是否在 memory 中占用空间,而 ZA2F2ED4F8EBC0661C21A291Z 已加载?

[英]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.

相关问题 静态方法和字段是否在定义它们的类的实例中占用内存? - Do Static Methods and Fields take up memory in an instance of the class they are defined in? 在Java中访问静态变量时,是否将类加载到内存中? - Does a class gets loaded in memory when a static variable is accessed in java? 是否将实例方法加载到每个对象或每个类的内存中? - Does an instance method get loaded into memory per object or per class? 如果没有静态方法或块,也没有对象,一个类可以获得多少内存 - How much memory does a class get if there is no static methods or blocks and no object 是否将静态变量与Class类对象一起加载 - Is static variable loaded along with Class class object Firebase侦听器会占用大量内存吗? - Does Firebase listeners take up huge memory? 何时将静态嵌套类(以及其中的静态成员)加载到内存中? - When is a static nested class (and static members therein) loaded into memory? 一个类在JVM中占用多少空间? - How much space does a class take in the JVM? 对于搜索每个对象或所有tile块的内存,内存更快 - which is faster a memory for a search through each object or a memory for all tile blocks 非静态变量在代码的哪一部分加载到内存中? - At which part of the code non-static variables are loaded into memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM