Memory Allocation
The idea behind garbage collection is, that all from the application
created objects are managed by the Runtime Environment. Therefore as a
developer, you do not have to care about memory management. The JVM is
using a heap for saving all objects. The size of the heap is configured
before the JVM starts and depends on the operating system. The garbagde
collection removes all objects which are not needed anymore from the
memory.
Garbage Collection Roots
If you want to understand garbadge collection, first you have to know
how needed objects, variables and code is stored during executing the
JVM. All starts with the Garbage Collection Roots. These are "special
objects" which are kept alive by the JVM itself. The GC roots can be:- The code of all needed classes is loaded to the heap by classloaders. These objects also have references to static variables.
- active Java threads
- lokale variables which belongs to the stack of a thread.
- native code (JNI)
Mark and Sweep Algorythms
To determine the objects which can be removed from memory, mark-and-sweep-algorithms are used. Starting from the garbage collection roots, all objects which are in use are marked. During the sweep-phase, all not marked objects are removed. If the algorythm will run during the application is running, old objects might be deleted or not deleted from the garbage collector by mistake. Hence, the application have to be stopped. To fasten the process, parallel or concurrent collectors are used.Parallel collector
The parallel collector uses multipe threads (multiple CPUs)
Concurrent collector
A mark-phase of a concurrent collector has multiple phases:
- Initial Marking: the GC-Roots are marked as "alive". All threads are stopped.
- Concurrent Mark: all from the GC-Roots reachable objects are marked. All threads are running
- All theads are stopped and objects which was created during phase 2 are marked.

Keine Kommentare :
Kommentar veröffentlichen