java 编译优化问题

作者&投稿:乾鹏 (若有异议请与网页底部的电邮联系)
~ java编译的结果是字节码而不是二进制,所以在运行时vm的优化才是重要的,包括VM的回收策略、分配给VM内存的大小都能在一定程度上影响性能。Sun的VM支持热点编译,对高频执行的代码段翻译的2进制会进行缓存,这也是VM的一种优化。

IBM JVM处理数学运算速度最快,BEA JVM处理大量线程和网络socket性能最好,而Sun JVM处理通常的商业逻辑性能最好。不过Hotspot的Server mode被报告有稳定性的问题。

Java 的最大优势不是体现在执行速度上,所以对Compiler的要求并不如c++那样高,代码级的优化还需要程序员本身的功底。

贴个java的运行参数:

Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-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.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
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>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-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

java.lang.instrument

-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

documentation)
-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虚拟机(JVM)参数配置说明

在Java、J2EE大型应用中,JVM非标准参数的配置直接关系到整个系统的性能。
JVM非标准参数指的是JVM底层的一些配置参数,这些参数在一般开发中默认即可,不需

要任何配置。但是在生产环境中,为了提高性能,往往需要调整这些参数,以求系统达

到最佳新能。
另外这些参数的配置也是影响系统稳定性的一个重要因素,相信大多数Java开发人员都

见过“OutOfMemory”类型的错误。呵呵,这其中很可能就是JVM参数配置不当或者就没

有配置没意识到配置引起的。

为了说明这些参数,还需要说说JDK中的命令行工具一些知识做铺垫。

首先看如何获取这些命令配置信息说明:
假设你是windows平台,你安装了J2SDK,那么现在你从cmd控制台窗口进入J2SDK安装目

录下的bin目录,然后运行java命令,出现如下结果,这些就是包括java.exe工具的和

JVM的所有命令都在里面。

-----------------------------------------------------------------------
D:\j2sdk15\bin>java
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-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.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
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>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-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

java.lang.instrument
-----------------------------------------------------------------------
在控制台输出信息中,有个-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

documentation)
-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.
-----------------------------------------------------------------------

JVM配置参数中文说明:
-----------------------------------------------------------------------
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
设置JVM初始化堆内存大小

10、-Xmx<size> set maximum Java heap size
设置JVM最大的堆内存大小

11、-Xss<size> set java thread stack size
设置JVM栈内存大小

12、-Xprof output cpu profiling data
输入CPU概要表数据

13、-Xfuture enable strictest checks, anticipating future default
执行严格的代码检查,预测可能出现的情况

14、-Xrs reduce use of OS signals by Java/VM (see

documentation)
通过JVM还原操作系统信号

15、-Xcheck:jni perform additional checks for JNI functions
对JNI函数执行检查

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.

eclipse本身是没有编译器的,就是你环境变量设置的jdk,MyEclipse自带的jdk也是sun的

空循环还是应该执行的,如果我想用空循环来达到某种目的,结果你给我弄掉了,这不。。。有些优化是仁者见仁,智者见智的事情,vs那样做未必好

for (int i = 0; i < 99999; i++)
for (int j = 0; j < 9999; j++)
a=1;
这样不就可以不会优化掉了呀,

java没有自动优化的功能,至于编译器,只有jdk带的那个javac。eclipse自带了jre的javac,和jdk里面的javac是一样的玩意,哪来的好不好?

比方做多线程的时候,有时候需要让一个线程等待下,就可以使用空循环,那你怎么优化?

优化要靠你自己平时的积累

编译器只负责编译和运行没啥关系。你这个问题纯属你自己造成的。不管那个平台编译出来的结果都是一样的,这个是跨平台的基础,要是编译出来的东西都不一样,那还怎么跨平台啊。编译器只负责检查语法错误,而运行的速度和你的硬件以及代码结构有关系。
从你这段代码看你是想测试代码运行时间,你中间加了一段没意义的空代码,最直接的提速就是删除空代码。你让人家白忙活还怪人家速度慢,这个慢是你自己造成的。

你要提高代码运行速度,有一个原则 大事化小 小事化了 不要在把一件很长的事情放到一段代码,不要做没意义的事情。不要把提高效率寄托在垃圾回收上,最好的代码是尽可能少的触发垃圾回收。特别是不要手动调用垃圾回收。


四方台区17848331805: java编译器的代码优化问题 -
蔽琼德瑞: 理论上的就不说了,你自己搜也能搜到很多.举个例子,你从一个方法a调用了另一个方法b.我们知道,在a和b之中是可以创建相同名称的变量的,比如都有int i = 0;这句话.这种现象的根本原因在于,方法的调用会产生中断,中断产生后,...

四方台区17848331805: java编译的问题 -
蔽琼德瑞: Java与c/c++的编译器对比实际上代表了最经典的即时编译器与静态编译器的对比.Java可能会下列原因导致输出本地代码有一些劣势:1. 首先,因为即时编译器运行占用的是用户程序的运行时间,具有很大的时间压力,它能提供的优化手段也...

四方台区17848331805: 如何优化JAVA代码及提高执行效率 -
蔽琼德瑞: 1、 尽量指定类的final修饰符带有final修饰符的类是不可派生的.在Java核心API中,有许多应用final的例子,例如java.lang.String.为String类指定final防止了人们覆盖length()方法.另外,如果指定一个类为final,则该类所有的方法都是final...

四方台区17848331805: 怎么做JAVA程序性能优化 -
蔽琼德瑞: 可供程序利用的资源(内存、CPU时间、网络带宽等)是有限的,优化的目的就是让程序用尽可能少的资源完成预定的任务.优化通常包含两方面的内容:减小代码的体积,提高代码的运行效率.本文讨论的主要是如何提高代码的效率.在Java...

四方台区17848331805: 如何优化JAVA程序开发,提高JAVA性能 -
蔽琼德瑞: 性能问题分很多种情况,服务器等硬件问题暂不考虑,一般代码方面最常见的有前端问题、后台逻辑处理和数据库相关问题 前端问题有以下几种方法: 梳理页面布局,除去多余的部分,减少浏览器渲染时间 合并JS、图片、css等文件,减少前台对后台的请求次数 压缩js和css文件,并对图片进行无损压缩,减少页面打开时的网络传输数据量 后台代码问题有以下几种:提高代码质量,减少不必要的集合或数组操作或者对象操作 优化逻辑处理过程 数据库相关问题有以下几种:优化sql语句,减少sql语句中全表扫描次数,尽量使用索引进行查询 优化代码,减少数据库连接开关次数和一次查询所查询的数据量

四方台区17848331805: Java编程优化写法从各方面来说 -
蔽琼德瑞: 从代码来看,其实你写这两种方式的代码是一样,执行性能很难区分的.你想想,你定义两个静态的,又是返回整数值,只不能你一种方式是少写代码,都执行的步骤还是一样.要想了解代码更多的性能问题,你可以在循环里做测试,也可以通过JAVA虚拟机来做测试等.还有让性能更好需要注意是变量不要定义是循环里,不要在循环NEW对象.尽量减少对数据库的交互次数.可以通过缓存和存储过程实现.在代码中多采取一种模式设计,减少一些代码重复执行等.

四方台区17848331805: 怎样利用Java开发高性能 -
蔽琼德瑞: 1、开发高性能Java程序的原则和方法:决定一个Java程序性能的好坏,是多方面的因素,包括前期的系统结构设计、底层平台架构设计、应用开发中的代码编写以及技巧的使用,系统平台的软件配置,系统平台的硬件性能等等.这里只给出代...

四方台区17848331805: 如何在编译java的时候,取消编译器对编译常量的优化? -
蔽琼德瑞: 遇到的问题是想重新编译某个java文件(比如A.java),里面有个常量(比如finalinta)和上次编译时不一样,但是另一个使用A.class的a的文件(比如B.java)由于在javac在上次编译的时候将当时的A.class里面的常量直接给内联了,所以就达不到想要的效果. 如果是这样的话,对于String可以使用.intern()来防止编译器进行优化,对于其他类型,可以要么不定义为常量,要么将常量定义为private,然后使用一个static方法来返回这个常量.

四方台区17848331805: 在类型检查前就进行编译优化会有什么问题 -
蔽琼德瑞: java编译的结果是字节码而不是二进制,所以在运行时vm的优化才是重要的,包括VM的回收策略、分配给VM内存的大小都能在一定程度上影响性能.Sun的VM支持热点编译,对高频执行的代码段翻译的2进制会进行缓存,这也是VM的一种优...

四方台区17848331805: java编译为什么那么慢 -
蔽琼德瑞: ecplipse编译慢,并不是说编译的工具慢,是由于工程代码很多,导致内存短时间产生不够的现象,表现出来的就是很慢.很多程序在进行大数据的计算或者数据库的操作,都需要很多的内存来计算或者保存数据,编译环境这时候就会很卡.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网