简体   繁体   English

这是垃圾收集还是没收?

[英]Is this garbage-collected or not?

Here is some code: 这是一些代码:

public class A {
  private volatile B b;

  public void methodC() {
    b.doSomething();
  }

  public void setB(B newB) {
    this.b = newB;
  }
}

'Thread 1' is executing b.doSomething() by executing methodC(). 'Thread 1'通过执行methodC()执行b.doSomething()。

At the same time 'thread 2' set a new B object into 'b'. 同时'线程2'将新的B对象设置为'b'。

My question is: 我的问题是:

Could the object previously refereced by 'b' be garbage-collected although doSomething() method on it is still executing? 先前由'b'引用的对象是否可以被垃圾收集,尽管它上面的doSomething()方法仍在执行?

No, because in order to call a member function you need to have a reference to the object. 不,因为要调用成员函数,您需要引用该对象。 Therefore the thread which is calling b.doSomething() will be holding a reference thus preventing garbage collection. 因此,调用b.doSomething()的线程将持有一个引用,从而防止垃圾回收。

Although check Jon Harrop's answer below for a situation when b could be GC 'd. 虽然检查Jon Harrop的答案如下,因为b可能是GC的情况。

Could the object previously refereced by 'b' be garbage-collected although doSomething() method on it is still executing? 先前由'b'引用的对象是否可以被垃圾收集,尽管它上面的doSomething()方法仍在执行?

Yes, the object previously refereced by 'b' might be garbage-collected even though the doSomething() method on it is still executing. 是的,先前由'b'引用的对象可能是垃圾收集的,即使它上面的doSomething()方法仍在执行。

You might expect this to be reachable while one of its methods is being executed because methods are implicitly passed the this pointer and you might assume that it is always spilled to the stack. 你可能认为this在执行它的方法之一,因为方法是隐式传递的是可以到达this指针,你可能认为它总是溢出到堆栈。 However, many optimizations can alter this and remove this from the set of global roots seen by the GC. 然而,许多优化可以改变这一点,并删除this从一组由GC看到全球根。

For example, if the body of the doSomething method ends with code that does not require this and the method is inlined then this might not be spilled from a register to the stack or its stack slot might be overwritten. 例如,如果doSomething方法的主体以不需要this代码的代码结束,并且该方法被内联,那么this可能不会从寄存器溢出到堆栈或其堆栈槽可能被覆盖。 In either case, the GC will no longer see this even though execution is still inside doSomething and, therefore, this might be garbage collected. 在任何一种情况下,即使执行仍在doSomething ,GC也不会再看到this ,因此, this可能是垃圾回收。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的对象在此类中被垃圾回收 - Why are my objects garbage-collected in this class 泄漏:ByteBuf.release() 在它被垃圾收集之前没有被调用 - LEAK: ByteBuf.release() was not called before it's garbage-collected 可以将Java RMI远程对象垃圾收集到次要收集中吗? - Can Java RMI remote objects be garbage-collected in a minor collection? 如果ServerSocket被垃圾收集,它的相关端口绑定会发生什么? - If a ServerSocket is garbage-collected, what happens to its associated port binding? 在类实例被垃圾回收之前调用Scanner#close()调用? - Scanner#close() invocation before class instance garbage-collected? 分析每个类的垃圾收集对象实例的数量 - Profiling number of garbage-collected object instances per class Java - 正在执行方法的对象可以被垃圾收集吗? - Java - Can objects which are executing methods be garbage-collected? 如果解除引用的Java对象启动仍在运行的线程,它仍然会被垃圾收集吗? - Does a dereferenced Java object still get garbage-collected if it started a thread which is still running? 在虚拟机(例如Dalvik)中运行的垃圾收集语言中的内存泄漏类型 - Types of memory leaks in a garbage-collected language running in virtual machines (ie, Dalvik) 成员变量可以从外部访问,但其声明类是用Java垃圾收集的? - Member variable is reachable from outside but its declaring class is garbage-collected in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM