您当前的位置:首页 > 互联网教程

java 编译优化问题

发布时间:2025-05-21 05:06:22    发布人:远客网络

java 编译优化问题

一、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.

二、怎么检验java是否安装成功

xp下:开始——>运行——>cmd确定——>java回车——>C:\Documents and Settings\Administrator>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 fil

A; separated list of directories, JAR archive

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 versi

-?-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.-age

see also,-agentlib:jdwp=help and-agentlib:

-agentpath:<pathname>[=<options>]

load native agent library by full pathname

-javaagent:<jarpath>[=<options>]

load Java programming language agent, see java

show splash screen with specified image

javac回车——>C:\Documents and Settings\Administrator>javac

用法:javac<选项><源文件>

-g:{lines,vars,source}只生成某些调试信息

-verbose输出有关编译器正在执行的操作的消息

-deprecation输出使用已过时的 API的源位置

-classpath<路径>指定查找用户类文件和注释处理程序的

-cp<路径>指定查找用户类文件和注释处理程序的

-sourcepath<路径>指定查找输入源文件的位置

-bootclasspath<路径>覆盖引导类文件的位置

-extdirs<目录>覆盖安装的扩展目录的位置

-endorseddirs<目录>覆盖签名的标准路径的位置

-proc:{none,only}控制是否执行注释处理和/或编译。

-processor<class1>[,<class2>,<class3>...]要运行的注释处理程序

-processorpath<路径>指定查找注释处理程序的位置

-d<目录>指定存放生成的类文件的位置

-s<目录>指定存放生成的源文件的位置

-implicit:{none,class}指定是否为隐式引用文件生成类文件

-encoding<编码>指定源文件使用的字符编码

-source<版本>提供与指定版本的源兼容性

-target<版本>生成特定 VM版本的类文件

-Akey[=value]传递给注释处理程序的选项

-J<标志>直接将<标志>传递给运行时系统

如果是这样的结果,说明你的Jdk安装成功了。大部分不成功的原因在于环境变量的配置不正确,所以一定要仔细设置环境变量的值。

没有更改路径安装的JDK,设置环境变量的值是这样的:

ClassPath.;C:\Program Files\Java\jdk1.6.0_24\lib\toos.jar;C:\Program Files\Java\jdk1.6.0_24\lib\dt.jar;C:\Program Files\Java\jdk1.6.0_24\bin

Path C:\Program Files\Java\jdk1.6.0_24\bin;

三、如何把java程序打包成linux下可运行的客户端

在JBuilder中生成EXE、可执行jar、带shell窗口的EXE

下面的图示是从JBuilder9截取,基本上对于版本7、8、9、X、200X来说类似。

点击Winzards菜单,选择Native Executable Builder菜单项目;

接下来进入Native Executable Builder Wizards,总共分7步:

第一步:如下图,在Name字段处给你的可执行程序命名,缺省名称为Native Executable,File是填写所需要生成的jar的名字和选择存放的位置,Compress the content of archive表示是否把jar文档里面的内容进行压缩,下面的checkbox代表是否在build项目的时候生成jar文档;

第二步:选择需要打包进jar的包、类、以及资源文件,也包括任何其他需要的文件,include class dependencies代表是否在jar包中包括所依赖的类(例如你用了一个第三方库的时候,以及其他引入的非java核心库的类),点击“Add Filters...”可以指定包括的包、类,点击“Add Files...”添加需要包括的资源文件等。

点击“Add Filter...”后出现Add Filters的窗口,如下图:

这里主要是定制过滤器,以确定包含哪些类,存在两种形式的Filter:include(包含)和exclude(排除)。可以直接对package包,也可以直接对class类,一般情况下,你在这里选择包含main方法那个主类就行了,前提是在上一个图中选中“包含依赖类”的checkbox。

Add Files就比较简单了,直接增加文件就行了,例如你程序需要的图片、配置文件等。

第三步:确定如何处理依赖库。下面图中的列表中列出来你在本项目中引入的库。

上面列出针对每个库的四种处理方式,按顺序依次是:

对于每个库的默认选项都是第一个,有时候你制作的Exe会出现找不到类的错误,就可能是这里没设置好。

第四步:设置jar文档的manifest(打包清单)项目。默认是包含,并自动创建一个,一般情况下按默认设置就行了。

第五步:选择确定应用程序主类的方法。

有两种方式可以指定:1、从指定的runtime configuration中选择一个运行配置来确定,runtime configuration就是下图中下拉表中的选项:

2、直接指定一个主类(就是包含main方法的那个类)。

第六步:选择需要生成的可执行程序类型。有五种类型可供选择:

2、带控制台console(就是dos窗口)的window exe类型;

在这一步就可以点击Finish生成了,第七步是可选的。

第七步:运行时配置选项的设置,一般情况下这一步是不需要的,高级用户也许需要,这一步干什么用的你可以看上面的说明^-^

然后的JB左侧项目文件浏览器中就会出现如下一个图标,名字是你在第一步中指定的名字:

在此项目上鼠标右击,选择make或者rebuild就OK了。

然后在你的项目目录中就出现了一个EXE文件。

这个过程的一个副产品就是同时生成了一个可执行的jar文件^-^

说明:这里生成的EXE和普通的window exe程序是有区别的,你鼠标右击该exe文件,看看属性,原来就是个压缩文件,不过这个压缩文件和普通的压缩文件有点区别,你自己慢慢看吧,还可以解压缩后看^-^,显然这不是真正的EXE文件,也就是说还需要JRE环境支持。

如果要生成真正的EXE可以使用其它工具,例如:jet-103-eval-win32.exe,该软件能够把java class编译成无需 JVM支持的 32位 Windows可执行文件。jet-103-eval-win32.exe这个文件的下载本站原来有,但是现在找不到这个程序了。

你还可以参考本站这个连接的《利用Eclipse和JSmooth生成java EXE可执行文件》,这种方式生成也很可能是和JB生成的一样,并非win32 exe程序。

当然jbuilder也能生成exe文件,但JSmooth显然小而方便,挺简单的。