我正在尝试加载 java 代理以使用 java.lang.instrument.Instrumentation 来测量对象的大小。这是我的 Java:
package com.example.memory.usage;
import java.lang.instrument.Instrumentation;
import java.util.*;
public class MemoryUsage {
public static void main(String[] args) throws Exception {
Random random = new Random();
Set<Integer> integerSet = new HashSet<>();
for(int i = 0; i < pixels; i++) {
if(random.nextDouble() < 0.20) {
integerSet.add(i);
}
}
System.out.println(ObjectSizeFetcher.getObjectSize(integerSet));
}
public static class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
System.out.println("Premain ... " + inst);
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
}
由于这是一个 Maven 项目,因此这是我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>memory-usage</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<finalName>memory-usage</finalName>
<plugins>
<!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!-- Jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.memory.usage.MemoryUsage</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Assembly -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.memory.usage.MemoryUsage</mainClass>
</manifest>
<manifestEntries>
<Premain-Class>java.lang.instrument.Instrumentation</Premain-Class>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
不,我不知道下一步该做什么。请帮忙。我跑了
$ mvn clean package
然后
$ java -jar target/memory-usage-jar-with-dependencies.jar
这给了我这个异常(exception):
Exception in thread "main" java.lang.NullPointerException
at com.example.memory.usage.MemoryUsage$ObjectSizeFetcher.getObjectSize(MemoryUsage.java:42)
at com.example.memory.usage.MemoryUsage.main(MemoryUsage.java:29)
我应该怎么做才能运行它?
最佳答案
启动机制被认为是 JVM/JRE 特定的。然而,package-summary of the instrumentation API告诉你:
<小时/>Command-Line Interface
An implementation is not required to provide a way to start agents from the command-line interface. On implementations that do provide a way to start agents from the command-line interface, an agent is started by adding this option to the command-line:
-javaagent:jarpath[=options]
jarpath is the path to the agent JAR file. options is the agent options. This switch may be used multiple times on the same command-line, thus creating multiple agents. More than one agent may use the same jarpath. An agent JAR file must conform to the JAR file specification.
launcher documentation还提供了提示:
<小时/>-javaagent:jarpath[=options]
Loads the specified Java programming language agent. For more information about instrumenting Java applications, see the
java.lang.instrument
package description in the Java API documentation at http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html
但请注意,您必须在 list 中指定正确的代理类。据我所知,您将 java.lang.instrument.Instrumentation 指定为 premain 类,这是无意义的。在您的代码中,包含 premain
方法的类是 com.example.memory.usage.MemoryUsage$ObjectSizeFetcher
,因此您应该指定该类...
关于Java新手: How to use Java Agent From the Command Line for Instrumentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709455/