• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

This paper introduces a Java virtual machine monitoring API developed by me

King Wang

1 月 3, 2022

I was looking at 《 In depth understanding of Java virtual machine 》 A Book , I plan to learn the knowledge of virtual machine , See 《 The first 4 Chapter Virtual machine performance monitoring and fault handling tools 》 When it comes to inspiration . The first is to understand what these monitoring tools can do ? And then we found some of the limitations , Then the view interprets part of the command source code , I also want to make a virtual machine monitoring tool , But it’s easy to think about making tools , If you can jdk The debugging library provided is modified and packaged into API, So, isn’t it easy for every developer to write their own virtual machine monitoring tools ? So I developed my first API:VmConsole-Api

  • github:https://github.com/tzfun/VmConsole-Api

Ask for a star to encourage (ಥ_ಥ)

VmConsole brief introduction

VmConsole-Api It’s a jvm Virtual machine performance monitoring API, take oracle jdk Provided tools.jar、sa-jdi.jar The package has been expanded , Some monitoring command results are encapsulated , You can easily read each parameter and result from the object . This class library does not simply use the runtime exec() call jps、jstat、jstack Wait for the order , It’s a deep expansion from the bottom , So you don’t need to configure java Environment variables can be passed through Java The code monitors the virtual machine .

at present VmConsole It has been released to Maven Central warehouse , Users can directly introduce Maven Rely on it , Of course not Maven Projects are also available from my Github Download Jar Package import project

Of course, there may be other companies or big guys that have developed virtual machine monitoring API, If there is, I will learn to practice , At the same time, I hope that friends who have relevant information will tell me about it .

jdk Limitations of monitoring tools

stay jdk There are many monitoring tools available in , such as jps、jstack、jstat etc. , And the image interface jconsole、visualvm etc. , But these tools Only for debugging , It’s really hard to use in your own projects , Unless calls exec() Method , But this method has a great impact on program performance .

stay tools.jar Package inside the view of these command class library source code can be found , It’s all function bodies that end at one time , Used a lot System.exit() Shut down the virtual machine and System.out.println() Output results , Availability is low . Readers can read my article to learn about the command execution process : In depth analysis from the source code point of view JVM Virtual machine monitoring tool

Yes tools.jar Functional expansion

tools.jar and sa-jdi.jar They are the two most important class libraries for virtual machine monitoring ,jdk/bin In the catalog jps、jconsole、visualvm And other programs are based on these two packages , stay tools.jar and sa-jdi.jar There are many ways to use it directly , They are equivalent to another layer of encapsulation of the virtual machine information acquisition class , But these packages are just One execution is the end of , Low reusability , The execution method in the class cannot be called directly . I overload by method 、 Class inheritance and other ways to expand some classes , Some properties and methods are private Of , Can’t expand , We refactor the whole class directly , such as beifengtz.vmconsole.tools.MyTool Class is right sa-jdi.jar Medium Tool Class methods are refactored .

I wrote it with extensibility in mind , In the original jdk The class method of is fixed. The output mode is System.out.println(), After adaptation, it provides print stream and object receiving results , More convenient for developers to expand their functions .

Easy to use

First of all, you need to put vmconsole-api.jar Package import into project , It can be direct import jar It can also be Maven.

Then you can directly use the commands encapsulated in it , For detailed instructions, please go to github see . Here we use jstat Command as an example .

If you use it locally jstat Command view vmId by 8804 Of gc situation , On the console, it turns out that :

If you use vmconsole Call function , Just call this in your own code :

The result of command execution is an object , For viewing purposes , After formatting it :

In addition to many interfaces for querying virtual machine information, there are Execute commands to other virtual machines Function of , Packaged in jcmd command . For example, the program you are using now is a virtual machine A, Another program in the virtual machine B, In the virtual machine A In the virtual machine B perform GC, So you just need to call :

Use scenarios

If you want to write a desktop version of the hypervisor ,VmConsole It can give you a lot of convenience . If you want to write a web Version of virtual machine monitoring tool , that VmConsole It’s a good choice for you , Because it’s not a simple execution exec() command , But from jdk Command bottom encapsulation , Compared to executing exec, It can give your program a lot of performance improvement .

发表回复