java 怎样获取instrumentation实例
发布时间:2025-05-21 18:41:14 发布人:远客网络
一、java 怎样获取instrumentation实例
找到这里就是因为我想获取一个 Instrumentation实例,但又不知道更优雅的方法,参见下面的代码。谁有更简洁的方法,请跟贴!
如何获取 instrumentation实例:
packagecom.taotling.instrument.insance;
importjava.lang.instrument.Instrumentation;
publicstaticInstrumentationinstrumentation;
publicstaticvoidpremain(StringagentArgs,Instrumentationinst){
publicstaticInstrumentationgetInstrumentation(){
}
将上面的代码打为JAR包,比如我取名为 agent.jar,添加 META-INF/MANIFEST.MF,内容为:
将上面的代码打为JAR包,比如我取名为 agent.jar,添加 META-INF/MANIFEST.MF,内容为:
Premain-Class:com.taotling.instrument.insance.MyJVMTIAgent
然后在你要调用 Instrumentation实例的类比如 Test类,通过-javaagent VM选项使用这个上面的JAR包:
然后在你要调用 Instrumentation实例的类比如 Test类,通过-javaagent VM选项使用这个上面的JAR包:
java-javaagent:"F:\test\myagent.jar"Test
Test类代码
importcom.taotling.instrument.insance.MyJVMTIAgent;
importjava.lang.instrument.Instrumentation;
publicstaticvoidmain(String[]args)throwsInterruptedException{
Instrumentationinstrumentation=MyJVMTIAgent.getInstrumentation();
System.out.println("instrumentationinstance:"+instrumentation);
System.out.println("thesizeofemptyObjectinstanceis"+instrumentation.getObjectSize(newObject())+"bytes.");
二、java 编译优化问题
java编译的结果是字节码而不是二进制,所以在运行时vm的优化才是重要的,包括VM的回收策略、分配给VM内存的大小都能在一定程度上影响性能。Sun的VM支持热点编译,对高频执行的代码段翻译的2进制会进行缓存,这也是VM的一种优化。
IBM JVM处理数学运算速度最快,BEA JVM处理大量线程和网络socket性能最好,而Sun JVM处理通常的商业逻辑性能最好。不过Hotspot的Server mode被报告有稳定性的问题。
Java的最大优势不是体现在执行速度上,所以对Compiler的要求并不如c++那样高,代码级的优化还需要程序员本身的功底。
Usage: java [-options] class [args...]
or java [-options]-jar jarfile [args...]
-client to select the"client" VM
-server to select the"server" VM
-hotspot is a synonym for the"client" VM [deprecated]
-cp<class search path of directories and zip/jar files>
-classpath<class search path of directories and zip/jar files>
A; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-version print product version and exit
require the specified version to run
-showversion print product version and continue
-jre-restrict-search|-jre-no-restrict-search
include/exclude user private JREs in the version search
-?-help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
-agentlib:<libname>[=<options>]
load native agent library<libname>, e.g.-agentlib:hprof
see also,-agentlib:jdwp=help and-agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see
-Xmixed mixed mode execution(default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by;>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/VM(see
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible(default)
-Xshare:on require using shared class data, otherwise fail.
在Java、J2EE大型应用中,JVM非标准参数的配置直接关系到整个系统的性能。
JVM非标准参数指的是JVM底层的一些配置参数,这些参数在一般开发中默认即可,不需
要任何配置。但是在生产环境中,为了提高性能,往往需要调整这些参数,以求系统达
另外这些参数的配置也是影响系统稳定性的一个重要因素,相信大多数Java开发人员都
见过“OutOfMemory”类型的错误。呵呵,这其中很可能就是JVM参数配置不当或者就没
为了说明这些参数,还需要说说JDK中的命令行工具一些知识做铺垫。
首先看如何获取这些命令配置信息说明:
假设你是windows平台,你安装了J2SDK,那么现在你从cmd控制台窗口进入J2SDK安装目
录下的bin目录,然后运行java命令,出现如下结果,这些就是包括java.exe工具的和
-----------------------------------------------------------------------
D:\j2sdk15\bin>java
Usage: java [-options] class [args...]
or java [-options]-jar jarfile [args...]
-client to select the"client" VM
-server to select the"server" VM
-hotspot is a synonym for the"client" VM [deprecated]
-cp<class search path of directories and zip/jar files>
-classpath<class search path of directories and zip/jar files>
A; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-version print product version and exit
require the specified version to run
-showversion print product version and continue
-jre-restrict-search|-jre-no-restrict-search
include/exclude user private JREs in the version search
-?-help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
-agentlib:<libname>[=<options>]
load native agent library<libname>, e.g.-agentlib:hprof
see also,-agentlib:jdwp=help and-agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see
-----------------------------------------------------------------------
在控制台输出信息中,有个-X(注意是大写)的命令,这个正是查看JVM配置参数的命
其次,用java-X命令查看JVM的配置说明:
运行后如下结果,这些就是配置JVM参数的秘密武器,这些信息都是英文的,为了方便
阅读,我根据自己的理解翻译成中文了(不准确的地方还请各位博友斧正)
-----------------------------------------------------------------------
D:\j2sdk15\bin>java-X
-Xmixed mixed mode execution(default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by;>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/VM(see
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible(default)
-Xshare:on require using shared class data, otherwise fail.
The-X options are non-standard and subject to change without notice.
-----------------------------------------------------------------------
-----------------------------------------------------------------------
1、-Xmixed mixed mode execution(default)
2、-Xint interpreted mode execution only
3、-Xbootclasspath:<directories and zip/jar files separated by;>
set search path for bootstrap classes and resources
设置zip/jar资源或者类(.class文件)存放目录路径
3、-Xbootclasspath/a:<directories and zip/jar files separated by;>
append to end of bootstrap class path
追加zip/jar资源或者类(.class文件)存放目录路径
4、-Xbootclasspath/p:<directories and zip/jar files separated by;>
prepend in front of bootstrap class path
预先加载zip/jar资源或者类(.class文件)存放目录路径
5、-Xnoclassgc disable class garbage collection
6、-Xincgc enable incremental garbage collection
7、-Xloggc:<file> log GC status to a file with time stamps
8、-Xbatch disable background compilation
9、-Xms<size> set initial Java heap size
10、-Xmx<size> set maximum Java heap size
11、-Xss<size> set java thread stack size
12、-Xprof output cpu profiling data
13、-Xfuture enable strictest checks, anticipating future default
执行严格的代码检查,预测可能出现的情况
14、-Xrs reduce use of OS signals by Java/VM(see
15、-Xcheck:jni perform additional checks for JNI functions
16、-Xshare:off do not attempt to use shared class data
17、-Xshare:auto use shared class data if possible(default)
18、-Xshare:on require using shared class data, otherwise fail.
尽可能的使用共享类的数据,否则运行失败
The-X options are non-standard and subject to change without notice.