user2434
user2434

Reputation: 6399

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 the objects that don't fall out of scope and remain unused are obsolete references. Correct me if I am wrong.

Upvotes: 25

Views: 5861

Answers (5)

Juliyanage Silva
Juliyanage Silva

Reputation: 2699

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.

Upvotes: 0

Bravo
Bravo

Reputation: 9047

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.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346437

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.

Upvotes: 14

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46415

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;
}

Upvotes: 5

user496934
user496934

Reputation: 4020

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

Upvotes: 2

Related Questions