1) What is Phantom
reference and where is it use?
A) Phantom reference
objects, which are enqueued after the collector determines that their referents
may otherwise be reclaimed. In other words when a phantom reference is
finalized the reference is queued in the ReferenceQueue. This can be used in
implementing connection pools, profiling and to check if a particular object is
garbage collected.
2) What is
WeakReference and where is it used?
A) Weak reference objects, which do not
prevent their referents from being made finalizable, finalized, and then reclaimed.
Weak references are most often used to implement canonicalizing mappings.
The difference is in exactly when the enqueuing
happens. WeakReferences are enqueued as soon as the object to which
they point becomes weakly reachable. This is before finalization or
garbage collection has actually happened; in theory the object could even be
"resurrected" by an unorthodox finalize() method, but
the WeakReference would remain dead. PhantomReferences are
enqueued only when the object is physically removed from memory, and
the get() method always returns null specifically to
prevent you from being able to "resurrect" an almost-dead object.
What good are PhantomReferences? I'm only aware of two
serious cases for them: first, they allow you to determine exactly when an
object was removed from memory. They are in fact the only way to
determine that. This isn't generally that useful, but might come in handy in certain
very specific circumstances like manipulating large images: if you know for
sure that an image should be garbage collected, you can wait until it actually
is before attempting to load the next image, and therefore make the dreaded OutOfMemoryError less
likely.
Second, PhantomReferences avoid a fundamental
problem with finalization: finalize() methods can
"resurrect" objects by creating new strong references to them. So
what, you say? Well, the problem is that an object which overrides finalize() must
now be determined to be garbage in at least two separate garbage collection
cycles in order to be collected. When the first cycle determines that it is
garbage, it becomes eligible for finalization. Because of the (slim, but
unfortunately real) possibility that the object was "resurrected"
during finalization, the garbage collector has to run again before the object
can actually be removed. And because finalization might not have happened in a
timely fashion, an arbitrary number of garbage collection cycles might have
happened while the object was waiting for finalization. This can mean serious
delays in actually cleaning up garbage objects, and is why you can get OutOfMemoryErrors even
when most of the heap is garbage.
The garbage collector enqueues soft, weak, and phantom
reference objects in different situations to indicate three different kinds of
reachability state changes. The meanings of the six reachability states and the
circumstances under which state changes occur are as follow:
- Strongly reachable - An object can be reached from the roots without traversing any reference objects. An object begins its lifetime in the strongly reachable state and remains strongly reachable so long as it is reachable via a root node or another strongly reachable object. The garbage collector will not attempt to reclaim the memory occupied by a strongly reachable object.
- Softly reachable - An object is not strongly reachable, but can be reached from the roots via one or more (un-cleared) soft reference objects. The garbage collector may reclaim the memory occupied by a softly reachable object. If it does so, it clears all soft references to that softly reachable object. When the garbage collector clears a soft reference object that is associated with a reference queue, it enqueues that soft reference object.
- Weakly reachable - An object is neither strongly nor softly reachable, but can be reached from the roots via one or more (un-cleared) weak reference objects. The garbage collector must reclaim the memory occupied by a weakly reachable object. When it does so, it clears all the weak references to that weakly reachable object. When the garbage collector clears a weak reference object that is associated with a reference queue, it enqueues that weak reference object.
- Resurrectable - An object is neither strongly, softly, or weakly reachable, but may still be resurrected back into one of those states by the execution of some finalizer.
- Phantom reachable - An object is not strongly, softly, nor weakly reachable, has been determined to not be resurrectable by any finalizer (if it declares a finalize() method itself, then its finalizer will have been run), and is reachable from the roots via one or more (uncleared) phantom reference objects. As soon as an object referenced by a phantom reference object becomes phantom reachable, the garbage collector will enqueue it. The garbage collector will never clear a phantom reference. All phantom references must be explicitly cleared by the program.
- Unreachable - An object is neither strongly, softly, weakly, nor phantom reachable, and is not resurrectable. Unreachable objects are ready for reclamation. more..
3) What is the difference between WeakReference and
PhantomReference?
A) A WeakReference is garbage collected when the next cycle
of garbage collection. This kind of references can be used for canonicalizing mappings.
PhantomReference is a reference which is pathomly reachable i.e the referent is
finalized and ready for garbage collection. The difference is that phantom
references are placed in a queue when the referent is garbage collected. The
WeakReference is queued when it is weakly referenced before or after marked for
garbage collection. A WeakReference can be resurrected but a PhantomReference
can never be.
4) What is referent and what is a reference, when will each
be garbage collected?
A) A referent is the actual object which is wrapped in a reference.
When the referent is no longer used as the reference to the referent is not
strongly reachable it will be garbage collected based on the ref. The reference
itself needs to be garbage collected as normally it will have a strong ref from
the holding object. To garbage collect the same the ReferenceQueue is used to
track the references whose referents are no longer used and can be cleared for
garbage collection in code.
5) What is a SoftReference and what is its use?
A) Soft reference objects, which are cleared at the
discretion of the garbage collector in response to memory demand. Soft references are most often used to
implement memory-sensitive caches.
6) What are the different regions in JVM?
A) There are 4 memory regions in a JVM, they are the
registers, the stack, the garbage-collected heap, and the method area.
7) What is the size of address word in JVM?
A) The size of an address in the JVM is 32 bits. So it can
access a maximum of 4 GB (2 ^ 32)
8) How many registers are there in the JVM?
A) Total 4 registers, they are the program counter, or pc
register, optop register,
frame register, and vars register(Except pc remaining three point to various
parts of the stack frame of the currently executing method).
9) Where does the byte code reside in the JVM?.
A) The method area is where the byte codes reside. The
program counter always points to (contains the address of) some byte in the
method area. The program counter is used to keep track of the thread of
execution. After a bytecode instruction has been executed, the program counter
will contain the address of the next instruction to execute. After execution of
an instruction, the JVM sets the program counter to the address of the
instruction that immediately follows the previous one, unless the previous one
specifically demanded a jump.
10) What is a Java Stack in JVM and why is it used?
A) The Java stack is used to store parameters for and
results of bytecode instructions, to pass parameters to and return values from
methods, and to keep the state of each method invocation. The state of a method
invocation is called its stack frame. The vars, frame, and optop registers
point to different parts of the current stack frame.
11) What are the 3 important sections of the Java Stack?
Explain them?.
A) There are three sections in a Java stack frame: the local variables, the execution
environment, and the operand stack. The
local variables section contains all the local variables being used by the
current method invocation. It is pointed to by the vars register. The execution environment section is
used to maintain the operations of the stack itself. It is pointed to by the
frame register. The operand stack is
used as a work space by bytecode instructions. It is here that the parameters
for bytecode instructions are placed, and results of bytecode instructions are
found. The top of the operand stack is pointed to by the optop register.
12) What is a root set in garbage collection in JVM?
A) The root set in a Java virtual machine is implementation
dependent, but would always include any object references in the local
variables and operand stack of any stack frame and any object references in any
class variables. Another source of
roots are any object references, such as strings, in the constant pool of
loaded classes. The constant pool of a loaded class may refer to strings stored
on the heap, such as the class name, superclass name, superinterface names,
field names, field signatures, method names, and method signatures. Another
source of roots may be any object references that were passed to native methods
that either hasn’t been "released" by the native method.
13) What are the 2 basic types of garbage collection?
A) Two basic approaches to distinguishing live objects from
garbage are reference counting and tracing.
Reference counting garbage collectors distinguish live objects from garbage
objects by keeping a count for each object on the heap. The count keeps track
of the number of references to that object. Tracing garbage collectors actually
trace out the graph of references starting with the root nodes. Objects that
are encountered during the trace are marked in some way. After the trace is complete,
unmarked objects are known to be unreachable and can be garbage collected.
14) Explain reference counting GC?
A) Reference counting was an early garbage collection
strategy. In this approach, a reference count is maintained for each object on
the heap. When an object is first created and a reference to it is assigned to
a variable, the object's reference count is set to one. When any other variable
is assigned a reference to that object, the object's count is incremented. When
a reference to an object goes out of scope or is assigned a new value, the
object's count is decremented. Any object with a reference count of zero can be
garbage collected. When an object is garbage collected, any objects that it
refers to have their reference counts decremented. In this way the garbage
collection of one object may lead to the subsequent garbage collection of other
objects.
15) What are the disadvantages of Reference Counting?
A) A disadvantage is that reference counting does not
detect cycles: two or more objects that refer to one another. An
example of a cycle is a parent object that has a reference to a child object
that has a reference back to the parent. These objects will never have a
reference count of zero even though they may be unreachable by the roots of the
executing program. Another disadvantage of reference counting is the overhead
of incrementing and decrementing the reference count each time.
16) Explain Tracing collectors?
A) Tracing garbage collectors trace out the graph of object
references starting with the root nodes. Objects that are encountered during
the trace are marked in some way. Marking is generally done by either setting
flags in the objects themselves or by setting flags in a separate bitmap. After
the trace is complete, unmarked objects are known to be unreachable and can be
garbage collected.
17) Give an example of tracing algorithm?
A) The basic tracing
algorithm is called "mark and sweep." This name refers to the two
phases of the garbage collection process. In the mark phase, the garbage
collector traverses the tree of references and marks each object it encounters.
In the sweep phase, unmarked objects are freed, and the resulting memory is
made available to the executing program. In the Java virtual machine, the sweep
phase must include finalization of objects.
18) What are compacting collectors?
A) Garbage collectors of Java virtual machines will likely
have a strategy to combat heap fragmentation. Two strategies commonly used by
mark and sweep collectors are compacting and copying. Both of these approaches
move objects on the fly to reduce heap fragmentation. Compacting collectors
slide live objects over free memory space toward one end of the heap. In the
process the other end of the heap becomes one large contiguous free area. All
references to the moved objects are updated to refer to the new location.
Updating references to moved objects is sometimes made
simpler by adding a level of indirection to object references. Instead of
referring directly to objects on the heap, object references refer to a table
of object handles. The object handles refer to the actual objects on the heap.
When an object is moved, only the object handle must be updated with the new
location. All references to the object in the executing program will still
refer to the updated handle, which did not move. While this approach simplifies
the job of heap defragmentation, it adds a performance overhead to every object
access.
19) What are copying collectors?
A) Copying garbage collectors move all live objects to a new
area. As the objects are moved to the new area, they are placed side by side,
thus eliminating any free space that may have separated them in the old area.
The old area is then known to be all free space. The advantage of this approach
is that objects can be copied as they are discovered by the traversal from the
root nodes. There are no separate mark and sweep phases. Objects are copied to
the new area on the fly, and forwarding pointers are left in their old
locations. The forwarding pointers allow the garbage collector to detect
references to objects that have already been moved. The garbage collector can
then assign the value of the forwarding pointer to the references so they point
to the object's new location.
20) Give an example of copy collector?
A) A common copying collector algorithm is called "stop
and copy." In this scheme, the heap is divided into two regions. Only one
of the two regions is used at any time. Objects are allocated from one of the
regions until all the space in that region has been exhausted. At that point
program execution is stopped and the heap is traversed. Live objects are copied
to the other region as they are encountered by the traversal. When the stop and
copy procedure is finished, program execution resumes. Memory will be allocated
from the new heap region until it too runs out of space. At that point the
program will once again be stopped. The heap will be traversed and live objects
will be copied back to the original region. The cost associated with this approach
is that twice as much memory is needed for a given amount of heap space because
only half of the available memory is used at any time.
21) What is the disadvantage of copy collectors?
A) One disadvantage of simple stop and copy collectors is
that all live objects must be copied at every
collection.
22) What are generational collectors and why are they
preferred?.
A) Taking into
account two facts that have been empirically observed in most programs in a
variety of languages:
- A) Most objects created by most programs have very short lives.
- Most programs create some objects that have very long lifetimes. A major source of inefficiency in simple copying collectors is that they spend much of their time copying the same long-lived objects again and again.
Generational collectors address
this inefficiency by grouping objects by age and garbage collecting younger
objects more often than older objects. In this approach, the heap is divided
into two or more sub-heaps, each of which serves one "generation" of
objects. The youngest generation is garbage collected most often. As most
objects are short-lived, only a small percentage of young objects are likely to
survive their first collection. Once an object has survived a few garbage
collections as a member of the youngest generation, the object is promoted to
the next generation: it is moved to another sub-heap. Each progressively older
generation is garbage collected less often than the next younger generation. As
objects "mature" (survive multiple garbage collections) in their
current generation, they are moved to the next older generation.
23) For what type of collection the Generational collection
can be used?
A) The generational collection technique can be applied to
mark and sweep algorithms as well as copying algorithms. In either case,
dividing the heap into generations of objects can help improve the efficiency
of the basic underlying garbage collection algorithm.
24) What are Adaptive collectors?
A) An adaptive
garbage collection algorithm takes advantage of the fact that some garbage
collection algorithms work better in some situations, while others work better
in other situations. An adaptive algorithm monitors the current situation on
the heap and adjusts its garbage collection technique accordingly. It may tweak
the parameters of a single garbage collection algorithm as the program runs. It
may switch from one algorithm to another on the fly. Or it may divide the heap
into sub-heaps and use different algorithms on different sub-heaps simultaneously.
25) What is the size of a reference in JVM?
A) The size is 4
bytes (32 bits).
26) What are daemon
threads in a JVM?
A) A daemon thread is ordinarily a thread used by the
virtual machine itself, such as a thread that performs garbage collection. The
application, however, can mark any threads it creates as daemon threads. The
initial thread of an application--the one that begins at main()--is a non-
daemon thread.
27) When can the JVM terminate?
A) A Java application continues to execute (the virtual
machine instance continues to live) as long as any non-daemon threads are still
running. When all non-daemon threads of a Java application terminate, the
virtual machine instance will exit. If permitted by the security manager, the
application can cause its own demise, by invoking the exit() method
of class Runtime or System.
28)

