mac java堆栈分析工具_mac java堆栈分析工具


mac java堆栈分析工具_mac java堆栈分析工具_02

Stack(线程私有)

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames.(线程与栈一一对应,栈中包含多个栈帧(即方法调用))
方法调用就是压栈过程,方法执行完毕就是出栈过程
一、局部变量表
存放内容:编译时可确定的基本数据类型、reference、returnAddress(指向一条字节码指令的地址)
空间单位:Slot(32位长度的内存空间)
占一个slot:boolean、byte、char、short、int、float、reference、returnAddress
占两个slot:(n,n+1):long、double
静态方法和实例方法在局部变量表的区别
若是实例方法,则0号索引位置默认为this,其余局部变量从1开始。
若是非实例方法,则从0开始

slot可复用:因为方法内定义的变量,其作用域并不一定会覆盖整个方法体,如果当前字节码pc计数器的值已经超出了某个变量的作用域,则这个变量对应的slot可以交给其他变量使用。所以为了节省栈帧空间,slot可以复用。

mac java堆栈分析工具_java_03

二、操作数栈(Operand Stack)
操作数栈的最大深度在编译是已经确定max_stack
在方法的执行过程中,会有各种字节码指令向操作数栈中写入或提取内容,也就是出栈/入栈操作。

三、动态链接(Dynamic Linking)
Keep in mind that the Java virtual machine contains a separate runtime constant pool for each class and interface it loads.
每个类或接口均有一个独立的常量池。

Each frame contains a reference to the run-time constant pool for the type of the current method to support dynamic linking of the method code. The class file code for a method refers to methods to be invoked and variables to be accessed via symbolic references. Dynamic linking translates these symbolic method references into concrete method references, loading classes as necessary to resolve as-yet-undefined symbols, and translates variable accesses into appropriate offsets in storage structures associated with the run-time location of these variables. This late binding of the methods and variables makes changes in other classes that a method uses less likely to break this code.
一个方法中的类文件代码是指被调用的方法和通过符号引用指向的变量。动态链接就是将这些符号引用转换为具体方法引用(直接引用),根据需要加载类以解析尚未定义的符号,并将变量访问转换为与这些变量的运行时位置关联的存储结构中的适当偏移量(也就是在内存中位置)。

加深理解可以看:https://www.artima.com/insidejvm/ed2/linkmod.html

PC Register(线程私有)

It is also associated by its thread. It basically is a address of current instruction is being executed. Since each thread some sets of method which is going to be executed depends upon PC Register. It has some value for each instruction and undefined for native methods. It is usually for keep tracking of instructions.

heap(线程共享)

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

Heap Memory can be accessed by any thread is further divided into three generations Young Generation,Old & PermGen(Permanent Generation). When object is created then it first go to Young generation(especially Eden space) when objects get old then it moves to Old/tenured Generation. In PermGen space all static & instance variables name-value pairs(name-references for object) are stored. Below is image showing heap structure of java.

PermGen is an abbreviation(缩写) for Permanent Generation and it’s a special heap space which is separate from the main Java heap where JVM keeps track of metadata of the classes which have been loaded. In Java 8, PermGen has been renamed to Metaspace - with some subtle differences.

mac java堆栈分析工具_Machine_04

Method Area(共享)

The Method area is a memory shared among all Java Virtual Machine Threads. It is created on virtual machine start-up and is loaded by classloaders from bytecode. The data in the Method Area stay in memory as long as the classloader which loaded them is alive.

Method Area is part of space in the Perm Gen and used to store class structure (runtime constants and static variables) and code for methods and constructors.

Runtime Constant pool is per class representation of constant Table. It contains all literals((string, integer, and floating point constants)defined at compiled time and references to types, fields, and methods. which is going to be solved at runtime.Because it holds symbolic references to all types, fields, and methods used by a type, the constant pool plays a central role in the dynamic linking of Java programs.

In other words, when a class, method or field is referred to, the JVM searches the actual address in the memory by using the runtime constant pool. It also contains constant values like string litterals or constant primitives.

String myString1 = “This is a string litteral”;
static final int MY_CONSTANT=2;

The method area stores:
就是.class文件,可以通过javap -v ClassName.class获取

  • class information (number of fields/methods, super class name, interfaces names, version, …)
  • the bytecode of methods and constructors.
  • a runtime constant pool per class loaded.

Native Method Stack(线程私有)

Native methods are those which are written in languages other than java. JVM implementations cannot load native methods and can’t rely on conventional stacks . It is also associated with each thread. In short it same as stack but it is used for native methods.

参考:https://www.hackerearth.com/zh/practice/notes/runtime-data-areas-of-java/
http://coding-geek.com/jvm-memory-model/