Jvm Memory Managment - amresh087/newronaRepos GitHub Wiki

                                     Jvm Memory Managment

image

  1. Jvm memory

  2. Heap, GC

  3. Monitoring

                                Componet of java memory management
    

image

Heap--> This is a place where all of object reside.

Perm Gen (Permanent Generation Space)--> This is special space for class loader. The Meta Data of the class/method. Perm gen is not part of heap memory. This is generate on run time by jvm. Inside perm gen that is contains special area called Method Area.

Method Area --> In this Area store static variable and constant and also store compile classes. Mean you can say its store class structure.

Native memory --> Native memory is where all other language specific code reside for example socket connection. They do not come in heap memory.

PC Register --> This is store the sequence of information all the instruction. which needs to execute next. There is one PC register created per thread. Its tell to threads what is need to do next.

Stack--> The stack is where all are local primitive variable and object reference variable and method parameter store and also store of sequence of the of the application.

                                  JVM High Level Components

image

young Genration --> young genration space is a place where all the newly created go in. This is part of the heap all the new object are created in young generation. Once young generation filled up the garbage collector kick up and this type of garbage collector is called minor garbage collector (Minor GC)

In the young generation there are multiple layer. In this Eden memory and two different survivor (survivor1 and survivor2)

The most importance in the Eden memory all the new object are inside the eden memory when Eden memory filled up with the object then minor GC kicked up and all the object which is got survived(mean which is in used) they move to survivor1 space.

The minor GC also check survivor if that time also object in use then move to another survivor 2 . So that make sure survivor space always empty.

The object are survived lots of cycle then get moved to the old generation.

Once the object do'nt get minor garbage collector reside in young generation then move to Old Generation this is high level flow. In another way you can say long lived object go in old generation.

The Garbage collection which happened in old generation is called Major garbage collector(Major GC). This is trying to collect all the object from old generation which is not used in any more.

Where are the Object Store Heap, Stack, Perm Gen Deeply Flow Diagram

image

Garbage Collection Step

There are three steps

  1. Marking
  2. Normal Deletion
  3. Deletion + compacting

Marking --> As part of marking garbage collector identify what object and which object is not use.

Normal Deletion --> As part of normal deletion these object has been identify as unused has remove fro heap memory.

Deletion + compaction--> Once object are remove from heap memory allocation is random. These memory allocation group to gather. So that memory allocation in future will be fast.

whose there three step follow there are getting two problem with simple mark and delete option.

problem 1--> The first one is that this is not efficient because most of newly creation object will become unused overtime.

problem 2--> Secondly object that are used in multiple garbage collector cycle are more likely to be in use for future cycle. So you not identify these we can directly mark and deletion Strategies instead of we have follow someting else.

So these problem resolved jvm provided different type of Garbage collection. So that can choose them.

This is main resign there are two generation type space created which is called young generation and old generation. So that these diff type of GC can act upon these young and old generation based on the heap memory uses.

Let see diff type of Garbage Collector and how do help these young and old generation to clean up jvm memory space.

1. Serial GC --> These serial Gc can be useful when we are mark and delete the object simply in young generation and old generation. These serial GC is useful in the client machine such as simple standalone application with smaller cpu.

For example our laptop. It is good for small application with low memory. Because it is just directly to identify object and delete from heap. So this is not a solution for large application.

2. Parallel GC--> Parallel gc are same as serial gc. However they they can response n number of thread for the young generation. The parallel gc is also called Throughput collector. Parallel GC uses single thread for Old Generation garbage collection.

For young gen use n thread and old gen 1 thread because there are less chances for the old gen to get GC that why only one thread in old gen.

3. Parallel Old GC--> The concept is same as parallel gc except that it uses multiple threads for both Young Generation and Old Generation garbage collection.

4. Concurrent Mark Sweep --> CMS Collector is also referred as concurrent low pause collector. It does the garbage collection for Old generation. CMS collector tries to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads.

CMS collector on young generation uses the same algorithm as that of the parallel collector. This garbage collector is suitable for responsive applications where we can’t afford longer pause times

5. G1 Garbage Collector --> The Garbage First or G1 garbage collector is available from Java 7. The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector.

Garbage First Collector doesn’t work like other collectors and there is no concept of Young and Old generation space. It divides the heap space into multiple equal-sized heap regions. When a garbage collection is invoked, it first collects the region with lesser live data

6. ZGC Answer

Perm Gen vs Meta Space

Meta space is new concept has been introduce in java 8. MataSpace is part of native memory(process level) Os level. These are the process level they do not merge in heap memory. The process size can go large since Meta space goes to the OS.

Metaspace is same like perm gen but in Perm gen we get out of memory error that does not happens with the Metaspace the space of the metaspace is allocated from the OS Memory.

Disadvantage--> This result brig down hole server too many classes loading on to the os memory is these is not enough space in OS then take down hole server. But you can control which we can provide the jvm.