directory

  • 1. The description of the JVM
  • 2. Class loading subsystem
  • 3. Run time data area
  • 4. Vm stack
  • 5. The heap area
  • 6. Method of area
  • 7. Object instantiation and memory access
  • Overview of garbage collection
  • 9. Classic garbage collector
  • 10. The G1 and ZGC

1 Vm stack features

1. Java Virtual Machine Stack (Java Virtual Machine Stack). Each thread creates a virtual machine Stack, which holds Stack frames for each Java method call. It is thread private

2. Lifecycle is consistent with threads

3. Stack is a fast and effective way to allocate storage, access speed is second only to PC register (program counter)

4. Function: In charge of the operation of Java program, it saves the local variables of the method, 8 basic data types, the reference address of the object, part of the result, and participates in the method call and return.

5. The JVM has only two direct operations on the Java stack

(1) Each method is executed with a push (push, push)

(2) Out of the stack after execution

6. There is no garbage collection problem for the stack, but there is definitely an OOM exception

2. Set the vm stack size

You can set the virtual stack size to be fixed or dynamic while the program is running

We can use the -xSS option to set the maximum stack space for a thread. The stack size directly determines the maximum reachable depth of a function call. (IDEA setting method: run-Editconfigurations -VM options Specifies the stack size -xss256K)

-Xss256k
Copy the code

3. The operating principle of the VM stack

  • 1. Each thread has its own Stack, and the data in the Stack is stored in the format of Stack frames

  • 2. Each method being executed on this thread has its own stack frame

  • 3. Stack frame is a memory block and a data set, which holds various data information in the process of method execution

  • 4. The JVM operates directly on the Java stack only two times, namely, the stack frame is pressed and removed, following the principle of “in, out, last in, first out”.

  • 5. In an active thread, there is only one active stack frame at a time. That is, only the stack Frame (top stack Frame) of the currently executing method is valid. This stack Frame is called the Current Frame, and the corresponding method is the Current Frame.

  • 6. All bytecode instructions run by the execution engine operate only on the current stack frame

  • 7. If another method is called in this method, a new stack frame is created and placed at the top of the stack as the new current stack frame.

  • 8. Stack frames contained in different threads are not allowed to reference each other, that is, it is impossible to reference another thread’s stack frame in another stack frame

  • 9. If the current method calls another method, when the method returns, the current stack frame will return the execution result of this method to the previous stack frame. Then, the virtual machine will discard the current stack frame, making the previous stack frame become the current stack frame again

  • Java methods have two ways of returning functions. One is to return a normal function using a return instruction. The other is to throw an exception. Either way, the stack frame will be ejected.

4. Whether local variables are thread-safe

/** * Are local variables defined in methods thread-safe? What is thread-safe? * If only one thread can manipulate this data, it must be thread-safe. * The data is shared if more than one thread operates on it. There are thread-safe issues if synchronized is not considered * * We know that StringBuffer is thread-safe and that synchronized is implemented in the source code. AbstractStringBuilder source code does not implement synchronized, which is unsafe in multithreaded situations * both AbstractStringBuilder * */
public class StringBuilderTest {

    //s1 is declared thread-safe; s1 is dead in method1
    public static void method1(a){
        StringBuilder s1 = new StringBuilder();
        s1.append("a");
        s1.append("b");
    }

    //stringBuilder: is unsafe because method2 can be called by multiple threads
    public static void method2(StringBuilder stringBuilder){
        stringBuilder.append("a");
        stringBuilder.append("b");
    }

    // the operation on s1 is thread unsafe and has a return value that may be shared by other threads
    public static StringBuilder method3(a){
        StringBuilder s1 = new StringBuilder();
        s1.append("a");
        s1.append("b");
        return s1;
    }

    The StringBuilder toString method creates a new String, and s1 dies internally
    public static String method4(a){
        StringBuilder s1 = new StringBuilder();
        s1.append("a");
        s1.append("b");
        return s1.toString();
    }

    public static void main(String[] args) {
        StringBuilder s = new StringBuilder();
        new Thread(()->{
            s.append("a");
            s.append("b"); }).start(); method2(s); }}Copy the code