Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the JVM native method stack & stack

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to understand the JVM local method stack & stack. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Today let's learn about the local method stack and the Java heap.

Local method stack

The local method stack plays the same role as the Java virtual machine stack, except that the Java virtual machine stack performs the Java method service for the virtual machine, while the local method stack performs the Native method service for the virtual machine.

Java reactor

The Java heap is the largest memory area managed by a virtual machine, and the heap is mainly used to store instances of objects. The Java heap is mainly divided into:

The old Survivor of the Eden (S0, S1 can also be from, to, belonging to the Cenozoic)

In the vast majority of cases, the object will be assigned to the new generation. After a new generation recovery, if the object is still alive, it will enter S0 or S1. After each new generation recovery, if the object survives, his age will be increased. When the object's age reaches a certain condition, it will enter the old age. Of course, if the object is very large, beyond the size of the new generation, the object may also be allocated directly in the old age.

With the development of JIT compiler and the maturity of escape analysis technology, techniques such as stack allocation and scalar substitution make object allocation in the heap less absolute.

Stack allocation

Allocation on the stack means that the objects that are private to those threads can be scattered on the stack. The advantage of allocation on the stack is that the objects are destroyed automatically with the end of the function call, without the intervention of the garbage collector, thus improving the performance of the system.

The basis of the allocation on the stack is to carry out escape analysis, and the purpose of escape analysis is to judge whether the scope of the object can escape from the function body.

Escape analysis can only be started in Server mode (the author does not do verification because his Java environment is Server environment. If readers are interested, you can use Client mode to do verification and then take screenshots and leave messages). The parameters are:

-XX:+DoEscapeAnalysispublic class EscapeAnalysis {

Public static class User {private int id = 1; private String name = "xiaoming";}

Public static void alloc () {User u = new User (); u.id = 5; u.name = "xiaohong";}

Public static void main (String [] args) {long start = System.currentTimeMillis (); for (int I = 0; I

< 100000000; i++) { alloc(); } long end = System.currentTimeMillis(); System.out.println(end - start); }} 执行以下命令将源码文件编译成字节码文件并且运行字节码 javac -d . EscapeAnalysis.javajava -server -Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:+PrintGC jvm/heap/EscapeAnalysis 由于Java8默认开启了逃逸分析,所以我们用-XX:-DoEscapeAnalysis移除逃逸分析看一下效果。 通过上图我们发现由于对象分配在堆上,但我们指定的最大堆为10m,不够存储我们大量User对象,因此虚拟机需要不停的进行垃圾回收来释放堆空间,程序执行时间928ms。 下面我们通过-XX:-EliminateAllocations将标量替换关闭,也就是说不允许对象打散分配在栈上。 java -server -Xmx10m -Xms10m -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+PrintGC jvm/heap/EscapeAnalysis 当我们关闭标量替换以后,会发现对象依旧不能在栈上分配。 下面让我们看一下当我们打开逃逸分析和标量替换(允许Use对象的id和name当做局部变量在栈上存储时)执行效果: java -server -Xmx10m -Xms10m -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+PrintGC jvm/heap/EscapeAnalysis

As can be seen from the above figure, our program has only one garbage collection, and the total running time 5ms is nearly 200times higher than that in the previous nearly 1s. It can be seen that due to the optimization of allocation on the stack, small objects can be allocated directly on the stack, avoiding the intervention of the garbage collector and greatly improving the performance of our program.

You need to enable both escape analysis and scalar substitution for allocation on the stack. These two parameters are enabled by default in Java8 Server mode.

The above is the editor for you to share how to understand the JVM native method stack & stack, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report