首页 > 编程知识 正文

优化java编译,java系统优化

时间:2023-12-27 22:26:40 阅读:324729 作者:OVND

本文目录一览:

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.

-Dname=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

-Xmssize set initial Java heap size

-Xmxsize set maximum Java heap size

-Xsssize 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:j2sdk15binjava

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.

-Dname=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:j2sdk15binjava -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

-Xmssize set initial Java heap size

-Xmxsize set maximum Java heap size

-Xsssize 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、-Xmssize set initial Java heap size

设置JVM初始化堆内存大小

10、-Xmxsize set maximum Java heap size

设置JVM最大的堆内存大小

11、-Xsssize 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.

楚雄java培训学校告诉你java中的编译与反编译?

一、什么是编译

1、利用编译程序从源语言编写的源程序产生目标程序的过程。

2、用编译程序产生目标程序的动作。编译就是把高级语言变成计算机可以识别的2进制语言,计算机只认识1和0,编译程序把人们熟悉的语言换成2进制的。编译程序把一个源程序翻译成目标程序的工作过程分为五个阶段:词法分析;语法分析;语义检查和中间代码生成;代码优化;目标代码生成。主要是进行词法分析和语法分析,又称为源程序分析,分析过程中发现有语法错误,给出提示信息。

二、什么是反编译

计算机软件反向工程(Reverseengineering)也称为计算机软件还原工程,是指通过对他人软件的目标程序(可执行程序)进行“逆向分析、研究”工作,以推导出他人的软件产品所使用的思路、原理、结构、算法、处理过程、运行方法等设计要素,某些特定情况下可能推导出源代码。反编译作为自己开发软件时的参考,或者直接用于自己的软件产品中。

三、Java类的编译与反编译

我们在最初学习Java的时候,会接触到两个命令:javac和java,那个时候我们就知道,javac是用来编译Java类的,就是将我们写好的helloworld.java文件编译成helloworld.class文件。

class文件打破了C或者C++等语言所遵循的传统,使用这些传统语言写的程序通常首先被编译,然后被连接成单独的、专门支持特定硬件平台和操作系统的二进制文件。通常情况下,一个平台上的二进制可执行文件不能在其他平台上工作。而Javaclass文件是可以运行在任何支持Java虚拟机的硬件平台和操作系统上的二进制文件。

那么反编译呢,就是通过helloworld.class文件得到java文件(或者说是程序员能看懂的Java文件)

四、什么时候会用到反编译

1、我们只有一个类的class文件,但是我们又看不懂Java的class文件,那么楚雄java培训认为可以把它反编译成我们可以看得懂的文件。

2、学习Java过程中,JDK的每个版本都会加入越来越多的语法糖,有些时候我们想知道Java一些实现细节,我们可以借助反编译。

从思路开始 Java如何实现条件编译的技巧

条件编译绝对是一个好东西。如在C或Cpp中,可以通过预处理语句来实现条件编译。代码如下:

#IFDEF DEBUG#UNDEF DEBUG#ENDIF #define DEBUG #IFDEF DEBUUG /*     code block 1     */#ELSE /*     code block 2   */#ENDIF

但是在JAVA中却没有预处理,宏定义这些东西,而有时我们可能需要条件编译。那么,在JAVA中,该如何实现条件编译呢?

我们来看一个例子。

编写一个helloworld程序。代码如下:

public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); }}

保存为Hello.java并编译,得到一个class文件,并且观察到文件大小是417字节。然后我们对这个文件进行反编译,用jd-gui。得到代码如下:

import java.io.PrintStream;public class Hello { public static void main(String[] paramArrayOfString)   { System.out.println("Hello, world!"); }}

得到这个有什么用呢?

现在我们再来对源代码进行修改,修改后的代码如下。

public class Hello { public static void main(String[] args) { if(false) { System.out.println("Hello, world!"); } }}

进行编译,这时我们再看它的大小,只有255字节。怎样?想到什么了吧?没错,编译器会对代码进行优化,对于条件永远为false的语句,JAVA编译器将不会对其生成字节码。

如何用JNI技术提高Java的性能详解

阻碍Java取得广泛使用的一个首要因素是Java程序的运行效率。Java是介于解释型和编译型之间的一种语言,同样的程序,假如用编译型语言C来实现,其运行速度通常要比Java快一倍以上。Java具有平台无关性,这使人们在开发企业级使用的时刻总是把它作为首要候选方案之一,但是性能方面的因素又大大削弱了它的竞争力。为此,提高Java的性能就显得十分主要。 疑问的提出 Sun公司及Java的支撑者们为提高Java的运行速度已经做出了许多全力,其中大多数集中在程序设计的要领和模式挑选方面。由于算法和设计模式的优化是通用的,对Java有效的优化算法和设计模式,对其他编译语言也基本同样适用,因此无法从根本上改动 Java程序与编译型语言在执行效率方面的差别。 JIT(Just In Time,及时编译)技能是个比较好的思想。它的基本原理是:最先议决 Java编译器把Java源代码编译成平台无关的二进制字节码。然后在Java程序真实执行之前,系统议决 JIT编译器把Java的字节码编译为本地化机器码。结尾,系统执行本地化机器码,节省了对字节码实行解释的时间。这样做的优点是大大提高了Java程序的性能,缩短了加载程序的时间;同时,由于编译的结果并不在程序运行间保存,因此也节约了存储空间。缺点是由于JIT编译器对所有的代码都想优化,因此同样也占用了许多时间。 动态优化技能是提高Java性能的另一个尝试。该技能试图议决把Java源程序直接编译成机器码,以充分使用 Java动态编译和静态编译技能来提高Java的性能。该要领把输入的Java源码或字节码转换为经历高度优化的可执行代码和动态库 (Windows中的. dll文件或Unix中的. so文件)。该技能能大大提高程序的性能,但却破坏了Java的可移植性。 JNI技能 实际上,有一种通常为咱们忽视的技能能够在很大程度上处理这个难题,那就是JNI(Java Native Interface, Java本地化要领 )。主张采用纯Java的人们通常反对本地化代码的运用,他们认为在Java程序执行的流程中调用C/C++程序会影响程序的可移植性和安全性。还有一些人认为JNI只是对过去混合编程技能的基本扩展,本来际目标是为了充分使用大量原有的C程序库。 本来,咱们不必拘泥于严格的平台独立性限定,因为采用JNI技能只是针对一些严重影响Java性能的代码段,该部分可能只占源程序的极少部分,所以几乎能够不思虑该部分代码在主流平台之间移植的工作量。同时,也不必过分担心类型匹配疑问,咱们完全能够控制代码不出现这种不正确。此外,也不必担心安全控制疑问,因为Java安全模型已扩展为准许非系统类加载和调用本地点法。根据Java规范,从JDK 1. 2开始,FindClass将设法找到与当前的本地点法关联的类加载器。假如平台有关代码属于一个系统类,则无需涉及任何类加载器; 否则,将调用适当的类加载器来加载和链接已命名的类。换句话说,假如在Java程序中直接调用C/C++语言产生的机器码,该部分代码的安全性就由Java虚拟机控制。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。