top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

what are the invisible objects in java?

+5 votes
559 views
what are the invisible objects in java?
posted Jan 9, 2014 by Prasad

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

invisible object are the object which have strong references associated with them but these references are not available to any thread stack, making object invisible to garbage collector.

Object life cycle:
Created
In use (The object is strongly reachable)
Invisible
Unreachable
Collected
Finalized
Deallocated

Example

public void execute()
{
  try
  {
    Object obj = new Object();
  }
  catch(Exception e)
  {
  e.printStackTrace();
  }

  while(true)
  {
    …………….
    ………….
    ………….
  }
}

In the above example when the execution of the code comes to the infinite while loop it may seem that the above Object referenced by obj is out of scope and is eligible for GC but in fact it lives in the same stack frame and occupies memory in heap area. Now if this Object(referred by obj) is very large in size and there are a lot like this before while loop then it can cause serious memory blockage and there are chances to get OutOfMemoryException.

Fix: To fix this we have to explicitly set the references to null after using them.

answer Jan 9, 2014 by Luv Kumar
...