---
title: "探索JVM运行状态的利器—JVMPI，HPROF_剑_百度空间  (引)"
author: "guo583"
date: "2012-04-12T10:29:29Z"
tags: []
categories: ["编程语言"]
slug: "jvm-jvmpi-hprof"
draft: false
lastmod: "2012-04-12T10:29:29Z"
id: 1484137
url: "https://guo583.iteye.com/blog/1484137"
---

．什么是JVMPI？Java Virtual Machine Profiler Interface。参考   
[http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html#overview](http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html#overview)

JVMPI可以做什么？它可以监控VM发生的各种事件。例如当JVM创建，关闭，Java类被加载，创建对象，或GC回收，等37种事件。既然是接口自然就是有一个头文件，\[JAVA\_HOME]\\include\\jvmpi.h。您可以开发自己的Profiler监控Java VM的发生的各种事件。

 

下面是我编写的使用JVMPI的例子，输出结果说明了为了运行一个Java类，JVM需要做的各种操作。

 

#include &lt;jvmpi.h&gt;

//1.&gt;cl -LDd -Zi -I. -IC:\\j2sdk1.4.2\_10\\include -IC

//   :\\j2sdk1.4.2\_10\\include\\win32 -Tp.\\jvmtrace.c -o jvmtrace.dll

//2.&gt;copy jvmtrace.dll C:\\j2sdk1.4.2\_10\\bin\\

//3.&gt;java -Xrunjvmtrace org.colimas.jni.test.JniTest

//jvmpi interface 全局指针

static JVMPI\_Interface \*jvmpi\_interface;

//时间通知处理函数

void notifyEvent(JVMPI\_Event \*event) {

switch(event-&gt;event\_type) {

       /\*非常可怕的输出，好奇就试试。

case JVMPI\_EVENT\_CLASS\_LOAD:

           fprintf(stderr, "trace&gt; Class Load : %s\\n", event-&gt;u.class\_load.class\_name);

           break;\*/

       case JVMPI\_EVENT\_ARENA\_DELETE :

           fprintf(stderr, "trace&gt; The heap arena :%d is deleted.\\n", event-&gt;u.delete\_arena.arena\_id);

           break;    

       case JVMPI\_EVENT\_ARENA\_NEW :

              fprintf(stderr, "trace&gt; The heap arena %s:%d is created.\\n",

                     event-&gt;u.new\_arena.arena\_name,

                     event-&gt;u.new\_arena.arena\_id);

           break;    

       case JVMPI\_EVENT\_GC\_FINISH :

              fprintf(stderr, "trace&gt; GC is finished. Used object:%d, space:%d, total object space:%d.\\n",

                     event-&gt;u.gc\_info.used\_objects,

                     event-&gt;u.gc\_info.used\_object\_space,

                     event-&gt;u.gc\_info.total\_object\_space);

           break;

       case JVMPI\_EVENT\_GC\_START :

              fprintf(stderr, "trace&gt;GC is started.\\n");

           break;

       case JVMPI\_EVENT\_HEAP\_DUMP :

              fprintf(stderr, "trace&gt; The heap dump begin %s,end %s.\\n",

                     event-&gt;u.heap\_dump.begin,

                     event-&gt;u.heap\_dump.end);

           break;

       case JVMPI\_EVENT\_JVM\_INIT\_DONE :

              fprintf(stderr, "trace&gt; JVM initialization is done.\\n");

           break;

       case JVMPI\_EVENT\_JVM\_SHUT\_DOWN :

              fprintf(stderr, "trace&gt; JVM is shutting down.\\n");

           break;

       case JVMPI\_EVENT\_THREAD\_END :

              fprintf(stderr, "trace&gt; A thread ends.\\n");

           break;

       case JVMPI\_EVENT\_THREAD\_START :

              fprintf(stderr, "trace&gt; The thread %s begins whose group is %s, parent is %s.\\n",

              event-&gt;u.thread\_start.thread\_name ,

              event-&gt;u.thread\_start.group\_name,

              event-&gt;u.thread\_start.parent\_name);

           break;

}                      

}

// profiler agent entry point

extern "C" {

JNIEXPORT jint JNICALL JVM\_OnLoad(JavaVM \*jvm, char \*options, void \*reserved) {

    fprintf(stderr, "trace&gt; initializing ..... \\n");

 

    // get jvmpi interface pointer

    if ((jvm-&gt;GetEnv((void \*\*)&jvmpi\_interface, JVMPI\_VERSION\_1)) &lt; 0) {

      fprintf(stderr, "trace&gt; error in obtaining jvmpi interface pointer\\n");

      return JNI\_ERR;

    }

    // initialize jvmpi interface

    jvmpi\_interface-&gt;NotifyEvent = notifyEvent;

    // enabling class load event notification

    //jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_CLASS\_LOAD, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_ARENA\_DELETE, NULL);

       jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_ARENA\_NEW, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_GC\_FINISH, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_GC\_START, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_HEAP\_DUMP, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_JVM\_INIT\_DONE, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_JVM\_SHUT\_DOWN, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_THREAD\_END, NULL);

    jvmpi\_interface-&gt;EnableEvent(JVMPI\_EVENT\_THREAD\_START, NULL);

    fprintf(stderr, "trace&gt; .... ok, dumping...\\n");

    if (jvmpi\_interface-&gt;RequestEvent(JVMPI\_EVENT\_HEAP\_DUMP,NULL)&lt;0)

    {

      fprintf(stderr, "trace&gt; error in obtaining jvmpi interface pointer\\n");

      return JNI\_ERR;

    }

    fprintf(stderr, "trace&gt; .... end\\n\\n");

    return JNI\_OK;

}

}

编译

&gt;cl -LDd -Zi -I. -IC:\\j2sdk1.4.2\_10\\include -IC

   :\\j2sdk1.4.2\_10\\include\\win32 -Tp.\\jvmtrace.c -o jvmtrace.dll

拷贝jvmtrace.dll到JAVA\_HOME\\bin

&gt;copy jvmtrace.dll C:\\j2sdk1.4.2\_10\\bin\\

运行

&gt;java -Xrunjvmtrace org.colimas.jni.test.JniTest

结果

trace&gt; initializing .....

trace&gt; .... ok, dumping...

trace&gt; .... end

trace&gt; The thread Finalizer begins whose group is system, parent is (null).

trace&gt; The thread Reference Handler begins whose group is system, parent is (nul

l).

trace&gt; The thread main begins whose group is main, parent is system.

trace&gt; JVM initialization is done.

trace&gt; The thread Signal Dispatcher begins whose group is system, parent is (nul

l).

hello JVM

trace&gt; A thread ends.

trace&gt; The thread DestroyJavaVM begins whose group is main, parent is system.

trace&gt; A thread ends.

trace&gt; JVM is shutting down.

2．HPROF又什么是？HPROF是J2SE自带的一个简单的profiler agent。 他是一个动态链接库文件，监控CPU的使用率，Heap分配情况等。将这些信息输出到文件或到socket。

java –Xrunhprof ToBeProfiledClass \[:help]|\[:=, ...]

例如：

&gt;java -Xrunhprof:heap=all org.colimas.jni.test.JniTest

在当前目录会生成java.hprof.txt文件。文件内记载了JVM运行时Heap的运行情况。