Repository navigation
jcmd jmap_old
ceragon edited this page Jul 27, 2022
·
5 revisions
$ jmap -histo:live pid
num #instances #bytes class name (module)
-------------------------------------------------------
1: 350965 612819840 [B (java.base@11.0.4)
2: 75791 21757000 [Ljava.lang.Object; (java.base@11.0.4)
3: 658665 21077280 java.util.HashMap$Node (java.base@11.0.4)
4: 384262 18444576 java.util.HashMap (java.base@11.0.4)
5: 175259 16354736 [Ljava.util.HashMap$Node; (java.base@11.0.4)
6: 84084 15047344 [I (java.base@11.0.4)
7: 131072 11534336 io.netty.buffer.PooledUnsafeDirectByteBuf
8: 287688 9206016 io.netty.util.Recycler$DefaultHandlepublic class JMap {
public static void main(String[] args) throws Exception {
if (option.equals("-histo")) {
histo(pid, "");
} else if (option.startsWith("-histo:")) {
histo(pid, option.substring("-histo:".length()));
} else if (option.startsWith("-dump:")) {
dump(pid, option.substring("-dump:".length()));
} else if (option.equals("-finalizerinfo")) {
executeCommandForPid(pid, "jcmd", "GC.finalizer_info");
} else if (option.equals("-clstats")) {
executeCommandForPid(pid, "jcmd", "GC.class_stats");
}
}
}public class JMap {
private static void histo(String pid, String options)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
String liveopt = "-all";
if (subopt.equals("live")) {
liveopt = "-live";
}
executeCommandForPid(pid, "inspectheap", liveopt, parallel);
}
private static void executeCommandForPid(String pid, String command, Object ... args)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
VirtualMachine vm = VirtualMachine.attach(pid);
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
try (InputStream in = hvm.executeCommand(command, args)) {
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
}
vm.detach();
}
}public abstract class HotSpotVirtualMachine extends VirtualMachine {
public InputStream executeCommand(String cmd, Object ... args) throws IOException {
return execute(cmd, args);
}
}public class VirtualMachineImpl extends HotSpotVirtualMachine {
String socket_path;
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
int s = socket();
connect(s, socket_path);
IOException ioe = null;
// 写协议类型和命令
writeString(s, PROTOCOL_VERSION);
writeString(s, cmd);
// 写入参数
for (int i=0; i<3; i++) {
if (i < args.length && args[i] != null) {
writeString(s, (String)args[i]);
} else {
writeString(s, "");
}
}
SocketInputStream sis = new SocketInputStream(s);
int completionStatus;
completionStatus = readInt(sis);
return sis;
}
}public class VirtualMachineImpl extends HotSpotVirtualMachine {
static native int socket() throws IOException;
static native void connect(int fd, String path) throws IOException;
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
}JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_socket
(JNIEnv *env, jclass cls)
{
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
return (jint)fd;
}JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_connect
(JNIEnv *env, jclass cls, jint fd, jstring path)
{
const char* p = GetStringPlatformChars(env, path, &isCopy);
struct sockaddr_un addr;
strncpy(addr.sun_path, p, sizeof(addr.sun_path) - 1);
connect(fd, (struct sockaddr*)&addr, sizeof(addr));
}JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_write
(JNIEnv *env, jclass cls, jint fd, jbyteArray ba, jint off, jint bufLen)
{
}