This set of Java Multiple Choice Questions & Answers (MCQs) on “Heap and Garbage Collection”.
1. Which of the following has the highest memory requirement? Answer: c 2. Where is a new object allocated memory? Answer: a 3. Which of the following is a garbage collection technique? Answer: b 4. What is -Xms and -Xmx while starting jvm? Answer: a 5. Which exception is thrown when java is out of memory? Answer: c 6. How to get prints of shared object memory maps or heap memory maps for a given process? Answer: a 7. What happens to the thread when garbage collection kicks off? Answer: c 8. Which of the below is not a Java Profiler? Answer: a 9. Which of the below is not a memory leak solution? Answer: c 10. Garbage Collection can be controlled by a program? Answer: b
a) Heap
b) Stack
c) JVM
d) Class
Clarification: JVM is the super set which contains heap, stack, objects, pointers, etc.
a) Young space
b) Old space
c) Young or Old space depending on space availability
d) JVM
Clarification: A new object is always created in young space. Once young space is full, a special young collection is run where objects which have lived long enough are moved to old space and memory is freed up in young space for new objects.
a) Cleanup model
b) Mark and sweep model
c) Space management model
d) Sweep model
Clarification: A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase. I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for allocating memory to new objects.
a) Initial; Maximum memory
b) Maximum; Initial memory
c) Maximum memory
d) Initial memory
Clarification: JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory. java -Xmx2048m -Xms256m.
a) MemoryFullException
b) MemoryOutOfBoundsException
c) OutOfMemoryError
d) MemoryError
Clarification: The Xms flag has no default value, and Xmx typically has a default value of 256MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError.
a) jmap
b) memorymap
c) memorypath
d) jvmmap
Clarification: We can use jmap as jmap -J-d64 -heap pid.
a) The thread continues its operation
b) Garbage collection cannot happen until the thread is running
c) The thread is paused while garbage collection runs
d) The thread and garbage collection do not interfere with each other
Clarification: The thread is paused when garbage collection runs which slows the application performance.
a) JVM
b) JConsole
c) JProfiler
d) Eclipse Profiler
Clarification: Memory leak is like holding a strong reference to an object although it would never be needed anymore. Objects that are reachable but not live are considered memory leaks. Various tools help us to identify memory leaks.
a) Code changes
b) JVM parameter tuning
c) Process restart
d) GC parameter tuning
Clarification: Process restart is not a permanent fix to memory leak problem. The problem will resurge again.
a) True
b) False
Clarification: Garbage Collection cannot be controlled by a program.