Java代码的优化方法有哪些
发布时间:2025-05-24 23:50:43 发布人:远客网络
一、Java代码的优化方法有哪些
说到代码优化,每个人或多或少都掌握一到两种方法,但是这样的方法对提升代码运行效率效果不大,最重要是对代码的重视和了解,这样才能提升代码的运行效率。在进行代码优化的过程中,方法是非常重要的,多掌握几种方法,根据代码的不同情况选择适合的方法进行优化。下面电脑培训为大家介绍Java代码优化的几种方法。
1、使用指定类、方法的final修饰符
具有final修饰符的类不可派生。在Java核心API中,有许多最终应用程序的例子,例如java.lang.String,整个类都是final。为类指定final修饰符允许继承类,并且为方法指定final修饰符允许覆盖该方法。如果将类指定为final,IT培训认为该类的所有方法都是final。Java编译器将寻找内联所有最终方法的机会。内联对于提高Java操作的效率非常重要。这可以将性能平均提高50%。
String对象的使用是非常重要的,StringBuilder/StringBuffer并不是字符串连接。由于Java虚拟机需要时间来生成对象,所以将来垃圾收集和处理这些对象可能需要一些时间。因此,生成太多对象将对程序的性能产生很大影响。
调用方法时传递的参数以及在调用中创建的临时变量都保存在堆栈中,速度更快。其他变量(如静态变量和实例变量)在堆中创建并且速度较慢。此外,昌平北大青鸟发现在堆栈中创建的变量,当方法完成运行时,内容消失,不需要进行额外的垃圾收集。
在Java编程过程中,在执行数据库连接和I/O流操作时要小心。使用后,北大青鸟北京嘉荟校区官网建议应及时关闭以释放资源。因为这些大型物体的操作会导致系统的大量开销,稍微粗心会导致严重的后果。
二、如何在编译java的时候,取消编译器对编译常量的优化
1、遇到的问题是想重新编译某个java文件(比如A.java),里面有个常量(比如finalinta)和上次编译时不一样,但是另一个使用A.class的a的文件(比如B.java)由于在javac在上次编译的时候将当时的A.class里面的常量直接给内联了,所以就达不到想要的效果。
2、如果是这样的话,对于String可以使用.intern()来防止编译器进行优化,对于其他类型,可以要么不定义为常量,要么将常量定义为private,然后使用一个static方法来返回这个常量。
三、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.