Between Java and C + + there is a "high wall" of memory management and garbage collection technology, people outside the wall want to go in, the wall people want to come out. ------from the in-depth understanding of Java virtual machines
As a Java programmer, because of the benefits of the virtual machine, in the development process generally do not care about memory allocation and garbage collection content, which let me in the C + + friends when chatting or communication, I always feel like a fake programmer, although we do not have to do these two things ourselves, However, it is necessary to understand the principle of this, in case of a memory overflow problem, it is not helpless. Here is a summary of some of the content through reading.
First, the Java Memory Area Division
Java Virtual machine in the process of executing Java program, it manages the memory area divided into different data regions, no data region has its own function, creation and destruction time, some with the start of the virtual machine, some with the user thread creation and end of the production and destruction, according to the The Java Virtual Machine specification stipulates that the memory managed by the Java Virtual machine will contain the following sections:
From the known, Java Virtual machine memory is mainly composed of thread-shared heap memory, method area and thread-private virtual machine stack, local method stack, and program counter five run-time data areas.
Second, the function of each run time data area introduction
1. Program counter
1) The program counter is the line number indicator of the byte code executed by the current thread, holding the next byte code position to be executed;
2) This memory area is the only area in the Java Virtual Machine specification that does not have any outofmemoryerror exceptions;
3) Thread Private
2. Java Virtual machine stack
1) Java Virtual machine stack is the memory model of the method execution, the popular point is that the execution method is the area of memory management.
When the method executes, a stack frame is created, which is used to store information such as local variables, operands, dynamic links and method exits in the method, and the process of calling execution of the method is a stack frame in the virtual machine stack to stack out the process;
2) thread-private, created as the thread begins, and destroyed by the end of the thread;
3) What is commonly referred to as Java stack memory is the local variable table in the Java Virtual machine stack, which holds the basic types that are known at compile time (Boolean, Byte, char, short, int, long, float, Dounle), Object references and ReturnAddress types. The size of the local variable is completed during the program compilation period and will not be changed during the execution of the method;
4) Exceptions that may occur in this area are:
Stackoverflowerror: A stack Overflow exception occurs when a thread requests a stack depth that exceeds the stack depth allowed by the Java Virtual machine;
OutOfMemoryError: A memory overflow exception occurs when the virtual machine stack does not request enough memory during the dynamic scaling process;
3. Local Method Stack
1) with the virtual machine stack, the local method stack is also the memory model that the Java method executes, except that the virtual machine stack executes the service for the Java program in the virtual machine, and the local method stack executes the service in the Java Virtual machine for the local method. In the virtual machine specification, there is no mandatory requirement for the language, usage and data structure of the methods used in the local method stack;
2) Thread Private
3) can throw two kinds of exceptions:
Stackoverflowerror: A stack Overflow exception occurs when a thread requests a stack depth that exceeds the stack depth allowed by the Java Virtual machine;
OutOfMemoryError: A memory overflow exception occurs when the virtual machine stack does not request enough memory during the dynamic scaling process;
4. Java Heap
1) heap memory is the largest piece of memory managed by a Java virtual machine;
2) thread sharing, created at virtual machine startup;
3) used to store object instances and arrays;
4) heap is the main area of garbage collector management, so it is also called GC heap;
5) heap memory is extensible and can be controlled by parameter--xmx and--XMS to set maximum heap memory and minimum heap memory;
6) throws a OutOfMemoryError exception if there is not enough memory in the heap to allocate the instance and the heap cannot be expanded
5. Method area
1) Same as heap memory, the method area is also a thread-shared memory area;
2) is used to store the class information that has been loaded by the virtual machine, constants, static variables, the real-time compiler compiled code and other data;
3) According to the Java Virtual Machine specification, the OutOfMemoryError exception is thrown when the method area fails to meet the memory allocation requirements
6. Direct Memory
This part of memory is neither the Java Virtual Machine Runtime data area nor the memory area defined in the Java VM specification, but this portion of memory is also used frequently and may cause outofmemoryerror exceptions. The allocation of native memory is not affected by the memory allocation of the Java Virtual machine, but when the sum of memory in each region of the virtual machine is greater than the native memory, it causes the OutOfMemoryError exception to occur dynamically.
The above is the memory model in the Java Virtual machine and the role or role played in the process of running the program. The following section summarizes the causes and workarounds for memory overflow exceptions in each memory area.
Java Virtual machine Family---java memory partitioning