Also works on core dumps, in the form of:
java -cp .:$JAVA_HOME/lib/sa-jdi.jar DirectMemorySize $JAVA_HOME/bin/java core.xxx
| $ java -version | |
| java version "1.6.0_30" | |
| Java(TM) SE Runtime Environment (build 1.6.0_30-b12) | |
| Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode) | |
| $ javac -classpath $JAVA_HOME/lib/sa-jdi.jar DirectMemorySize.java | |
| $ jps | |
| 18486 GroovyStarter | |
| 23135 Jps | |
| $ java -classpath .:$JAVA_HOME/lib/sa-jdi.jar DirectMemorySize `pgrep java` | |
| Attaching to process ID 18486, please wait... | |
| Debugger attached successfully. | |
| Server compiler detected. | |
| JVM version is 20.5-b03 | |
| NIO direct memory: (in bytes) | |
| reserved size = 0.000000 MB (0 bytes) | |
| max size = 4069.000000 MB (4266655744 bytes) |
| import java.io.*; | |
| import java.util.*; | |
| import sun.jvm.hotspot.memory.*; | |
| import sun.jvm.hotspot.oops.*; | |
| import sun.jvm.hotspot.debugger.*; | |
| import sun.jvm.hotspot.runtime.*; | |
| import sun.jvm.hotspot.tools.*; | |
| import sun.jvm.hotspot.utilities.*; | |
| public class DirectMemorySize extends Tool { | |
| public void run() { | |
| // Ready to go with the database... | |
| try { | |
| long reservedMemory = getStaticLongFieldValue("java.nio.Bits", "reservedMemory"); | |
| long directMemory = getStaticLongFieldValue("sun.misc.VM", "directMemory"); | |
| System.out.println("NIO direct memory:"); | |
| System.out.printf(" reserved size = %fMB (%d bytes)\n", toM(reservedMemory), reservedMemory); | |
| System.out.printf(" max size = %fMB (%d bytes)\n", toM(directMemory), directMemory); | |
| } catch (AddressException e) { | |
| System.err.println("Error accessing address 0x" | |
| + Long.toHexString(e.getAddress())); | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static long getStaticLongFieldValue(String className, String fieldName) { | |
| InstanceKlass klass = SystemDictionaryHelper.findInstanceKlass(className); | |
| LongField field = (LongField) klass.findField(fieldName, "J"); | |
| return field.getValue(klass); | |
| } | |
| public static double toM(long value) { | |
| return value / (1024 * 1024.0); | |
| } | |
| public String getName() { | |
| return "directMemorySize"; | |
| } | |
| public static void main(String[] args) { | |
| DirectMemorySize tool = new DirectMemorySize(); | |
| tool.start(args); | |
| tool.stop(); | |
| } | |
| } |
Package "sun.jvm.hotspot.memory.*" is in the $JAVA_HOME/lib/sa-jdi.jar. Did you add this jar to CLASSPATH?
收藏了
学习
HI 我执行完一次之后。kill -9 DirectMemorySize 的运行进城后,想再次使用就一直连接不上去 。这是为什么呢?
使用-e或-v参数时只显示和不加参数时同样的信息,上面例子上给出的额外信息一直长时间无法显示,是什么原因?
[root@cass047202 ~]# java -classpath .:/usr/install/java/lib/sa-jdi.jar DirectMemorySize -e 2904
Attaching to process ID 2904, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.51-b03
NIO direct memory: (in bytes)
reserved size = 32686.010643 MB (34273766296 bytes)
max size = 27566.686493 MB (28905765856 bytes)
使用环境 CentOS 6.5 64bit,物理内存32G,Jdk7,Xmx8G
还有一个问题,按你这篇文章http://hllvm.group.iteye.com/group/topic/27945 提到的,如果参数 -XX:MaxDirectMemorySize没有设置,默认应该是约等了-Xmx8g这个值,为何这里显示是将近27G?
Any love to port this to JDK8?
he is addicted for yaba for long time he always motivated new one comer.
can it works on java8?
tool.run(args) modify to tool.execute(args);
同问 它可以在java8上运行吗
irectMemorySize.java:3: package sun.jvm.hotspot.memory does not exist
import sun.jvm.hotspot.memory.*;
^
Couldn't find this package from some reason. Running on Mac OSX