Latest posts by khushvinder (see all)
- How to get top five CPU and Memory consuming processes from zabbix using SNMP and PS linux command - September 26, 2019
- How to monitor Services/Processes in zabbix using SNMP? - August 17, 2019
- How to get overall CPU utilization in zabbix - June 15, 2019
Garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program.
Memory leaks is a type of resource leak that occurs when a computer program incorrectly manages memory allocations. In such a way that memory which is no longer needed is not released. In object-oriented programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running code.
Heap is the area of memory used for dynamic allocation. In most configurations the operating system allocates the heap in advance to be managed by the JVM while the program is running. How new object getting space from heap given below steps:
1. Creating new object in the program getting next memory portion (index) from the heap.
2. When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system.
What is GC root and How GC root helps to clean unused object/reference?
GC (Garbage Collector) roots are objects special for garbage collector. Garbage collector collects those objects that are not GC roots and are not accessible by references from GC roots so that type of objects are eligible for Garbage Collector.
There are four kinds of GC roots in Java:
- Local variables are kept alive by the stack of a thread. This is not a real object virtual reference and thus is not visible. So local variables are GC roots.
- Active Java threads are always considered live objects and are therefore GC roots. This is especially important for thread local variables.
- Static variables are referenced by their classes. This fact makes them de facto GC roots. Classes themselves can be garbage-collected, which would remove all referenced static variables.
- JNI References are Java objects that the native code has created as part of a JNI call. Objects thus created are treated specially because the JVM does not know if it is being referenced by the native code or not.
How GC root helps to clean unused object/reference?
Internally GC using some algorithm to reclaim the memory like Mark and Sweep is one from them. Basically it works on two main points like below:
- The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
- All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.
In Hotspot heap is broken up into smaller parts or generations. The heap parts are: Young Generation, Old or Tenured Generation, and Permanent Generation.
The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. Minor collections can be optimised assuming a high object mortality rate. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.
The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.
The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.
For Out Of Memory Error please follow the Out Of Memory Error post.
1,531 total views, 1 views today