Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created January 11, 2012 07:18
Show Gist options
  • Save rednaxelafx/1593521 to your computer and use it in GitHub Desktop.
Save rednaxelafx/1593521 to your computer and use it in GitHub Desktop.
An Serviceability-Agent based tool to see stats of NIO direct memory, as an alternative on JDK6 without JMX support for direct memory monitoring. Only works on JDK6; to work on JDK7 will need some tweaking because static variables are moved to Java mirror
$ 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();
}
}

Also works on core dumps, in the form of:

java -cp .:$JAVA_HOME/lib/sa-jdi.jar DirectMemorySize $JAVA_HOME/bin/java core.xxx
@asafm
Copy link

asafm commented Aug 1, 2013

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

@caoxudong
Copy link

@asafm

Package "sun.jvm.hotspot.memory.*" is in the $JAVA_HOME/lib/sa-jdi.jar. Did you add this jar to CLASSPATH?

@fuyou001
Copy link

收藏了

@JinhouLiu
Copy link

学习

@allwmh
Copy link

allwmh commented Oct 28, 2014

HI 我执行完一次之后。kill -9 DirectMemorySize 的运行进城后,想再次使用就一直连接不上去 。这是为什么呢?

@yikebocai
Copy link

使用-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?

@simmel
Copy link

simmel commented Jan 15, 2015

Any love to port this to JDK8?

@reza987
Copy link

reza987 commented Dec 19, 2017

21124143_1044357149040746_2070796490_n
55666

@reza987
Copy link

reza987 commented Dec 19, 2017

he is addicted for yaba for long time he always motivated new one comer.

@ydx2008
Copy link

ydx2008 commented Mar 5, 2018

can it works on java8?

@Rayn-liuwei
Copy link

tool.run(args) modify to tool.execute(args);

@zhanghongwang
Copy link

同问 它可以在java8上运行吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment