The basic structure

  1. The Class loading subsystem is responsible for loading Class information from the file system or network.
  2. The method area holds loaded Class information and runtime constants.
  3. The Java heap is established when the VIRTUAL machine is started. It is the most important memory working area for Java programs. All object instances of several households are stored in the heap and shared by threads.
  4. Direct memory NIO allows Java to use system memory directly, which is faster than Java heap access and is suitable for scenarios with frequent reads and writes.
  5. The garbage collection system collects method areas, the Java heap, and direct memory, primarily the Java heap. Garbage collection system will run in the background of the program, find, identify and release garbage objects;
  6. Java stack Each thread has a private Java stack, which holds thread-local variables, method parameters, and is closely related to Java method calls and returns.
  7. The local method stack is similar in structure to the Java stack except that it is used for local method calls. The Java virtual machine allows Java to call local methods directly (usually written in C).
  8. The PC register Program Counter, which is thread private, points to the method being executed by the current thread. If it is a local method, the PC register value is undefined.
  9. The execution engine is responsible for executing the virtual machine’s bytecode.

The Java heap

Different garbage collection mechanisms can lead to different Java heap structures, the most common of which are as follows:

The entire Java heap is divided into the new generation and the old generation, for details refer to the subsequent garbage collection mechanism.

-Xmx indicates the maximum pair space of the system. For example -XMx64m, the maximum available heap space of the system is 64M.

Example: Get the maximum available heap memory of the current system

public static void main(String[] args) {
    long maxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024;
    System.out.println("-Xmx" + maxMemory + "m");
}
Copy the code

Java stack

As with the stack definition in data structures, first in, last out. The main content of The Java stack is the stack frame. For each method call, there is a corresponding stack frame on the stack, and the corresponding stack frame is off the stack when the call ends. Stack frame mainly contains local variable table, operand stack and frame data.

-Xss indicates the maximum stack space of the system. For example, Xss256k indicates that the maximum available stack space of the system is 256K.

Example: Obtain the maximum stack depth of the system (observe the output result by adjusting the stack size and local variables)

public class Test {

    private int count = 0;

    public void recursion(a) {
        long a = 1L, b = 2, c = 3, d = 4, e = 5, f = 6;
        count++;
        recursion();
    }

    public static void main(String[] args) {
        Test test = new Test();
        try {
            test.recursion();
        } catch(Throwable e) { System.out.println(test.count); e.printStackTrace(); }}}Copy the code

Methods area

In JDK 1.6 and JDK 1.7, the method area, also known as Perm, stores system class information, including fields, methods, constant pools, and so on.

-xx :PermSize, -xx :MaxPermSize Specifies the initial permanent size and the maximum permanent size.

Example: Test the maximum number of classes to be created (relying on the third-party library Cglib)

public class Test {

    public static void main(String[] args) {
        int count = 0;
        try {
            while (true) {
                Enhancer enhancer = new Enhancer();
                // Specify that caching is not applicable
                enhancer.setUseCache(false);
                enhancer.setSuperclass(Test.class);
                enhancer.setCallback(new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        return null; }}); enhancer.create(); count++; }}catch(Exception e) { System.out.println(count); e.printStackTrace(); }}}Copy the code

In JDK 1.8, the permanent section was completely removed and replaced by the metadata section, specified with the -xx :MaxMetaspaceSize parameter, which is located in system memory.