JVM memory structure-program counter, virtual machine stack

Program counter

1.1 Definition

  • The function is to remember the execution address of the next jvm instruction

  • Features

    • Thread private, that is, each thread has its own program counter
    • No memory overflow
  • In essence, the essence of the program counter is a register, which plays a role in the process of converting the binary code of the jvm into the machine code.

Virtual machine stack

  • definition

    • Java Virtual Machine Stacks (Java virtual machine stack)
    • Each thread can only have one virtual machine stack (thread private), corresponding to the method currently executed
  • effect

    • Whenever a thread executes a method, it is pushed into the virtual machine stack; whenever a method is executed, it is ejected from it.
  • Thinking

    • Are local variables in methods thread safe?
      • If the local variables in the method are not accessed from the role of the escape method, it is thread-safe
      • If the local variable references the object and escapes the scope of the method, thread safety needs to be considered.
      • The core is whether there is a situation of multi-threaded access.
  • Overflow of stack memory

    • Too many stack frames cause memory overflow (often)

      • For example, when the end condition of recursion is not well specified, there will be too many problems that cause overflow.
    • Excessive stack frame size causes stack memory overflow

  • Thread runtime diagnosis

    • Use top to locate the process that appears (for example, CPU usage is too high)

    • ps H -eo pid,tid,% cpu | grep process id

    • jstack process id

      • The problematic thread can be found according to the thread id, and the source line number of the problem code can be further located

Guess you like

Origin blog.csdn.net/qq_45788043/article/details/112203248