JMM Flashcards
How is virtual heap space divided in Java?
young gen :
—– eden - object created using new keyword
—– survivor - objects which have survived after java garbage collection from Eden space
old (Tenured) gen - objects which survived after garbage collection from Young Generation
perm gen - metadata of classes (static methods, method names, JVM objects
after java 8 - no more eperm gen => Metaspace (not heap)
Metaspace VS Perm gen
Metaspace has unlimited default maximum size
Strings used to be kept on the PermGen
What difference between float and BigDecimal. How they store the data?
BigDecimal - more precision
BigInteger - before point and int after point
Java object references
Strong - object will not be garbaged
Weak - object will be garbaged if only weak ref available
Soft - object will be garbaged only in case of memory need
Phantom - object will be garbaged only but also put in queue on finalize mehtod (you will be notified when object is garbage collected)
deep copy vs shallow copy
deep copy - all the objects in the tree are deeply copied, so the copy isn’t dependant on any earlier existing object that might ever change
shallow copy - only copy field values and therefore the copy might be dependant on the original object
Disadvantages of setting heap size too high
extended caching warmup times,
problems with fragmentation
problems with garbage collection
How to force GC be executed?
no way to force
calling System.gc() may lead to GC
What are memory leaks?
objects present in the heap
that are no longer used,
but the garbage collector is unable
to remove them from memory
What is variable shadowing
a variable in a certain scope
(eg., inside a loop)
has the same name
as a variable declared in outer scope.
Types of garbage collectors
Serial Garbage Collector - freezing all the application threads while doing garbage collection. uses just a single thread for garbage collection
Parallel Garbage Collector - the default garbage collector of the JVM. uses multiple threads for garbage collection.also freezes all the application threads while performing garbage collection.
CMS Garbage Collector - Concurrent Mark Sweep
freezes only if:
1 - marking in the tenured generation space.
2 - change in heap memory while garbage collection.
uses more CPU, but less stops
G1 Garbage Collector separates the heap memory into regions and does collection within them in parallel.better support heaps larger than 4GB.
Default garbage collectors
Java 7 - Parallel GC
Java 8 - Parallel GC
Java 9 - G1 GC
Java 10- G1 GC
Java 11- Z GC
Strong, Weak, Soft and Phantom References
Strong - default. Object not eligible for GC
Weak - object is eligible for GC
Soft - hold object until memory is available
Phantom - object is directly eligible for GC (used to know when an object is removed from memory)