java之垃圾收集器和匿名类

你猜 阅读:94 2025-06-02 22:19:02 评论:0

我们有代码:

Iterator<Object> it = new Collection<Object>(){/*...implementation...*/}.iterator(); 

问:垃圾收集器会移除我创建的表示收集的对象吗?形式上,我们没有对此对象的引用,但是 it (Iterator <Object>)仍然与我们匿名类的对象内部相连。

换句话说,考虑代码:

Iterator<Object> it = new Collection<Object>(){             // String (1) 
 
    private Object[] array; 
 
    public Iterator<Object> iterator(){ 
        /*Here, an iterator that references this.array is returned 
        + Performing its tasks*/ 
    }  
 
    /* + other implementation...*/  
 
}.iterator(); 

然后 GC 会删除第一行创建的对象,我们客观上没有链接吗? //String (1)

对于那些特别喜欢写伪答案的人,这里是我的迭代器的代码:

Iterator<Object> it = new Collection<Object>() { // String (1) 
 
        private Object[] array2; 
 
        @Override 
        public Iterator<Object> iterator() { 
            return new Iterator<Object>() { 
                Object[] array; 
                { 
                    array = array2; 
                } 
                @Override 
                public boolean hasNext() { 
                    // We do what is necessary 
                    return false; 
                } 
                @Override 
                public Object next() { 
                    // We do what is necessary 
                    return null; 
                } 
            }; 
        } 
 
        /* + other implementation... */ 
}.iterator(); 

小的附加问题:

是否可以将 Collection 实现中的“array2”重命名为“array”?那么如何在 Iterator 的实现中使用它呢?

return new Iterator<Object>() { 
    Object[] array; 
    { 
        array = array2;   // array = array ? It doesn't work. How refer to array above 
    } 
    // and so on... 

关于重复...不是this question .也许看起来像,但我想收到关于删除的问题的答案。这会发生还是不会发生,为什么? + 收到附加问题的答案很重要。 That question可以帮助别人理解我的问题的答案,但不能帮助我。

请您参考如下方法:

只要外部类是匿名内部类,如果给它起相同的名称 array,就不能访问外部类的字段。但是,第二个 array 字段的整个维护已过时,因为它不会阻止内部(最)类维护对其外部实例的引用,如 Do anonymous classes always maintain a reference to their enclosing instance? 中所述。 .

这两个问题的解决方法是一样的,要么不要使用匿名内部类,而是使用命名嵌套类,或者将其移动到范围内没有外部实例的工厂方法中:

    Iterator<Object> it = new AbstractCollection<Object>() { 
        private Object[] array; 
        @Override 
        public Iterator<Object> iterator() { 
            return getIterator(array); 
        } 
        @Override 
        public int size() { 
            throw new AssertionError("no-one's gonna call this here"); 
        } 
    }.iterator(); 
… 
 
static Iterator<Object> getIterator(final Object[] array) { 
    return new Iterator<Object>() { 
        @Override 
        public boolean hasNext() { 
            // We do what is necessary 
            return false; 
        } 
        @Override 
        public Object next() { 
            // We do what is necessary 
            return null; 
        } 
    }; 
} 

当然,这还有两个问题

  1. 当我们已经知道我们要做的就是调用 iterator() 方法时,Collection 实现的意义何在?为什么不首先调用 getIterator 方法?

  2. 如果我们在这一点上,为什么不直接调用 Arrays.asList(array).iterator()(请记住,这不会复制数组)?

也就是说,拥有引用并不一定能防止垃圾回收。考虑 The Java® Language Specification, §12.6.1 :

A reachable object is any object that can be accessed in any potential continuing computation from any live thread.

Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a Java compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner.

请注意,这甚至适用于 Iterator 实例,在将其代码内联到调用方(即循环)后,其存储可能会被回收,因此它只对数组起作用。但是您不会注意到这一点,因为优化器当然会确保程序的行为不会改变。


标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号