Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is meant by obsolete reference in java?

Tags:

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.

like image 415
user2434 Avatar asked Jul 27 '11 10:07

user2434


2 Answers

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.

like image 163
Michael Borgwardt Avatar answered Oct 01 '22 19:10

Michael Borgwardt


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; } 
like image 44
Saurabh Gokhale Avatar answered Oct 01 '22 20:10

Saurabh Gokhale