1. Garbage collection mechanism (GC)
1) Java memory allocation
In Java programs, both code and data need to be stored in memory, and the Java program requires memory to be managed by the JVM, and the JVM divides the requested memory logically into three regions: heap, stack, method area. These three regions are used to store different data.
The heap is used to store the objects created, and the JVM has only one heap area (heap) shared by all threads, the heap does not hold the base type and object references, only the object itself, each object contains a corresponding class of the information (class is to get instructions);
Only the base data type and the reference variable of the object in the heap space are saved in the stack, the data in each stack (the base type and the object reference variable) is private, the other stacks are inaccessible, the stack is divided into 3 parts: the basic type variable area, the execution environment context, the Operation instruction area (hold the operation instruction).
The method area is used to hold information about the class, and when the Java program runs, it first loads the bytecode information of the class file through the class loader, which is parsed and loaded into the method area. The various information (including methods) of a class is stored in the method area, using only one copy of the method.
2) Garbage Collection
The garbage collector (garbage COLLECTION,GC) is a thread that comes with the JVM (a program that runs automatically) to reclaim objects that no references point to.
GC threads start tracking from reference variables in the stack to determine which memory is in use, and if the GC cannot track down a block of memory, then the GC will assume that the memory is no longer in use, which is recyclable. However, Java programmers do not have to worry about memory management because the garbage collector is automatically managed.
GC recycling is transparent to programmers, and does not necessarily immediately reclaim existing unreferenced objects. In general, you can call the System.GC () method when we need GC threads to reclaim useless objects immediately. This method is used to suggest that the JVM immediately dispatches GC threads to reclaim resources, but does not guarantee immediate recycling.
This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1655668
Java object-Oriented learning notes--7 (GC)