Tuesday, 7 June 2016

Garbage Collection in Java

Question: What is responsiveness and throughput parameter for any web application?

Responsiveness
Throughput
Refers to how quickly an application or system responds with a requested piece of data.

Focuses on maximizing the amount of work by an application in a specific period of time.
Long pause times are not acceptable for applications that focus on throughput.

High pause times are acceptable for applications that focus on throughput.

The focus is on responding in short periods of time.
Since high throughput applications focus on benchmarks over longer periods of time, quick response time is not a consideration
E.g. how quickly UI responds, how fast a website returns a page, how fast a database query is returned etc.

The number of transactions/jobs/queries/etc completed in a given time.

Question: Which things are stored in Heap and in stack or diff between Heap Space and Stack Space?

Java Heap space
Java Stack space
It is used by java runtime to allocate memory to objects and JRE classes.
(It is used by all the parts of the application.)

It is used for execution of a thread.
(It is used only by single thread of execution.)

New objects are created in Heap space.
(Stack memory contains the references to it.)

It stores Method specific vales, local primitives and references to other objects in the heap that are getting refereed from the method.

Objects stored in the heap are globally accessible.

Stored values cannot be accessible by other threads.

GC runs on the heap memory to free the memory used by objects that does not have any references.
(Heap memory lives from the start till the end of the application/freed by GC)

In the end of the execution of the method, the unused memory becomes available for next method.
(Stack memory is short-lived)

Heap space is bigger than stack space.
(Heap memory is divided into parts like young generation, old generation and perm gen.)

In comparison with each other, access from Heap memory is slower than Stack memory.

Stack size is very small compared to Heap memory.

Because of the simple design (LIFO) and less size, stack memory is faster than Heap.


When Heap memory is exhausted, java runtime throws java.lang.OtOfMemoryError

When stack memory is full, java runtime throws java.lang.StackOverFlowError

For tuning, we can mainly use following parameters.
–Xms(define the startup sizeof the Heap)
–Xmx(define maximum size of the Heap)

For tuning, we can mainly use following parameters.
- Xss(define the stack memory size)

Question: What is Automatic Garbage Collection? When does an object become eligible for garbage collection?

Unlike other programming languages, process of deallocating memory in Java is not manual. It is handled by Garbage Collector- a system thread. Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.

An object becomes eligible for GC when there is no live reference for that object or it can not be reached by any live thread. If two objects have cyclic dependency on each other and both have no live reference then also both objects are eligible for GC.

Question: What are Young (New) Generation, Old Generation, Permanent Generation, Eden memory, S0 Survivor memory, S1 Survivor memory, Minor Garbage collection, Major Garbage Collection and “Stop the world” events? Which things are stored in these areas? What is permanent Generation? What are the changes in JDK 1.8? What is Metaspace?

Young Generation: Young generation is the place where all the new objects are created. When young generation is filled completely, garbage collection is performed. This garbage collection is known as Minor GC. Young generation is also known as New Generation. Young Generation is divided into following sections: Eden Memory and two Survivor Memory (S0 Survivor memory and S1 Survivor memory) spaces.

Eden Memory:  Most of the newly created objects are located in the Eden memory space. When it is filled with objects, minor GC is performed and objects are moved to one of the survivor space.

Survivor Memory: Survivor memory has two sections S0 Memory space and S1 Memory space which are also known as From-space and To-space. Each minor GC done in the young generation increments the age of objects in the survivor spaces. When an object has survived a sufficient number of minor GCs (Defaults vary but normally start at 15) it will then be promoted to the Old Generation.

Old Generation: The Old Generation is usually much larger than the New Generation and it contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old Generation memory when it’s full. Old Generation Garbage Collection is called Major GC and usually takes longer time.

Stop the World events: All application threads are stopped during the execution of GC. This event is known as Stop the World event. Minor GC and major GC both are this type of events. Often a major collection is much slower because it involves all live objects.

Permanent generation (PermGen): It contains metadata required by the JVM to describe the classes and methods used in the application. Java SE classes and methods are also stored in it. Runtime, it is populated by JVM, based on classes used in the application. The permanent generation is included in a full garbage collection.

Java 8: PermGen is no longer exists in Java 8. Java designers have redesigned the architecture and they removed PermGen and introduced new area so called Metaspace




No comments:

Post a Comment