Java Memory allocation Policy
-
static Storage (method area): mainly holds static data, global static data, and constants. This block exists when the program is compiled , and it is present during the entire run of the program.
-
Stack area: When a method is executed, local variables in the method body, including the underlying data types, references to Objects , are created on the stack, and the memory held by those local variables is automatically freed at the end of the method execution. Because the stack memory allocation operation is built into the processor's instruction set, it is highly efficient, but the allocated memory capacity is limited .
-
Heap Area: also known as dynamic memory allocation, usually refers to the program when it runs directly new memory, that is, an instance of the object. The Java garbage collector will be responsible for recycling this part if it is not in use.
Conclusion
- The base data types and references for local variables are stored in the stack, and the referenced object entities are stored in the heap. -because they belong to the variables in the method, the life cycle ends with the method.
- member variables are all stored in the heap (including basic data types, referenced and referenced object entities)-because they belong to a class, the class object is eventually used by new.
How Java is managing memory
GC in order to properly dispose of objects, the GC must monitor the running state of each object, including the application, reference, reference, assignment, etc. of the object, which the GC needs to monitor. The object's state is monitored to release the object more accurately and in a timely manner, and the fundamental principle of releasing the object is that the object is no longer referenced.
, let's give an example of how memory management is represented by a graph. For every moment of the program, we have a graph representing the memory allocations of the JVM.
Java uses a graph-like approach to memory management, which eliminates the problem of reference loops, such as having three objects and referencing each other, so that GC can reclaim them as long as they are unreachable from the root process. The advantage of this approach is that the precision of managing memory is high, but the efficiency is low. Another common memory management technique is the use of counters, such as the COM model, which uses counter-mode management artifacts, which are low-precision rows (difficult to handle circular references) compared to a forward graph, but perform efficiently.
What is a memory leak in Java
Two characteristics, first of all, these objects are accessible , that is, in the graph, the existence of the path can be connected to it, and secondly, these objects are useless , that is, the program will no longer use these objects. If the object satisfies both conditions, these objects can be judged as a memory leak in Java.
The function of GC is SYSTEM.GC (), but according to the Java language Specification, this function does not guarantee that the JVM's garbage collector will execute.
Typically, aGC's thread has a lower priority level . There are a number of policies that the JVM calls the GC, some of which are used to a certain extent, the GC starts to work, there are timed executions, there is a gentle execution of the GC, and some interrupt-execution GC. such as online games, users do not want the GC to suddenly interrupt application execution and garbage collection, then we need to adjust the GC parameters, so that the GC can be a gentle way to free memory, such as garbage collection decomposition into a series of small steps to execute, Sun provides the hotspot JVM support this feature.
Also gives a typical example of a Java memory leak,
New Vector (ten); for (int1; i++) {new Object (); V.add (o); NULL ; }
Java Basic Knowledge Learning