开发者

what is meant by obsolete reference in java?

I am reading Effective Java and I came across this term, "Obsolete Reference". When is a reference obsolete reference? I am assuming that all t开发者_Python百科he objects that don't fall out of scope and remain unused are obsolete references. Correct me if I am wrong.


An obsolete reference (as used in the book, though it's not a widely used technical term) is one that is kept around but will never be used, preventing the object it refers to from being eligible for garbage collection, thus causing a memory leak.


An obsolete reference is simply a reference that will never be dereferenced again.

From Effective Java,

Holding onto obsolete references constitutes memory leaks in Java. This is also termed as unintentional object retention.

Nulling out a reference to remove obsolete references to an object is good, but one must not overdo it. The best way to eliminate an obsolete reference is to reuse the variable in which it was contained or to let it fall out of scope.

E.g for removing obsolete reference,

public Object pop() {
    if (size == 0)
        throw new EmptyStackException();
    Object result = elements[--size];
    elements[size] = null; // Eliminate obsolete reference
    return result;
}


You are right. Basically, an obsolete reference is something which does-not affect the later flow of the program and should be set to null to aid garbage collection.

For example ;

String a="some value"; . . . . . . //some processing here //once done do this a=null; //a is obsolete reference


unused objects , which are still having the references (may be not intentionally) and those reference are not refereed by your application/program/code , then that reference are obsolete reference. since the reference is still their for these unused objects , GC , is not possible for these objects and the objects which are inside those objects , and these leads memory leak issues.


A simple explanation is that even if the value popped from the method, that value is still being pointed inside the array by its index. So even if the program really is done with the passed-by reference copy, it will still be in the heap because of the elements array used in the stack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜