Java Heap分为3个区,Young,Old和PermanentYoung保存刚实例化的对象当该区被填满时,GC会将对象移到Old区Permanent区则负责保存反射对象,本文不讨论该区
JVM的Heap分配可以使用-X参数设定,
-Xms 初始Heap大小
-Xmx java heap最大值
-Xmn young generation的heap大小
JVM有2个GC线程第一个线程负责回收Heap的Young区第二个线程在Heap不足时,遍历Heap,将Young 区升级为Older区Older区的大小等于-Xmx减去-Xmn,不能将-Xms的值设的过大,因为第二个线程被迫运行会降低JVM的性能
为什么一些程序频繁发生GC?有如下原因:
1)程序内调用了System.gc()或Runtime.gc()
2)一些中间件软件调用自己的GC方法,此时需要设置参数禁止这些GC
3)Java的Heap太小,一般默认的Heap值都很小
4)频繁实例化对象,Release对象此时尽量保存并重用对象,例如使用StringBuffer()和String()
如果你发现每次GC后,Heap的剩余空间会是总空间的50%,这表示你的Heap处于健康状态许多Server端的Java程序每次GC后最好能有65%的剩余空间
经验之谈:
1)Server端JVM最好将-Xms和-Xmx设为相同值为了优化GC,最好让-Xmn值约等于-Xmx的1/3[2]
2)一个GUI程序最好是每10到20秒间运行一次GC,每次在半秒之内完成[2]
注意:
1)增加Heap的大小虽然会降低GC的频率,但也增加了每次GC的时间并且GC运行时,所有的用户线程将暂停,也 就是GC期间,Java应用程序不做任何工作
2)Heap大小并不决定进程的内存使用量进程的内存使用量要大于-Xmx定义的值,因为Java为其他任务分配内存,例如每个线程的Stack等
2.Stack的设定
每个线程都有他自己的Stack
-Xss 每个线程的Stack大小
Stack的大小限制着线程的数量如果Stack过大就好导致内存溢漏-Xss参数决定Stack大小,例如-Xss1024K如果Stack太小,也会导致Stack溢漏
3.硬件环境
硬件环境也影响GC的效率,例如机器的种类,内存,swap空间,和CPU的数量
如果你的程序需要频繁创建很多transient对象,会导致JVM频繁GC这种情况你可以增加机器的内存,来减少Swap空间的使用[2]
4.4种GC
第一种为单线程GC,也是默认的GC,该GC适用于单CPU机器
第二种为Throughput GC,是多线程的GC,适用于多CPU,使用大量线程的程序第二种GC与第一种GC相似,不同在于GC在收集Young区是多线程的,但在Old区和第一种一样,仍然采用单线程-XX:+UseParallelGC参数启动该GC
第三种为Concurrent Low Pause GC,类似于第一种,适用于多CPU,并要求缩短因GC造成程序停滞的时间这种GC可以在Old区的回收同时,运行应用程序-XX:+UseConcMarkSweepGC参数启动该GC
第四种为Incremental Low Pause GC,适用于要求缩短因GC造成程序停滞的时间这种GC可以在Young区回收的同时,回收一部分Old区对象-Xincgc参数启动该GC
4种GC的具体描述参见[3]
参考文章:
1. JVM Tuning. http://www.caucho.com/resin-3.0/performance/jvm-tuning.xtp#garbage-collection
2. Performance tuning Java: Tuning steps
http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,1604,00.html
3. Tuning Garbage Collection with the 1.4.2 JavaTM Virtual Machine .
-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
3-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
4-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
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 documentation)
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.
-----------------------------------------------------------------------
怎么用这这些参数呢?其实所有的命令行都是这么一用,下面我就给出一个最简单的HelloWorl的例子来演示这个参数的用法,非常的简单
-----------------------------------------------
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
编译并运行:
D:\j2sdk15\bin>javac HelloWorld.java
D:\j2sdk15\bin>java -Xms256M -Xmx512M HelloWorld
Hello World!
----------------------------
分享到:
相关推荐
JVM调优实践 ⼀、JVM调优准备⼯作 1. 机器环境: 机器 CPU 内存 单机 4C 16G 2. 默认启动参数 JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256 -XX:MetaspaceSiz e=128m -XX:MaxMetaspaceSize=320m" JAVA_...
1.1 JVM调优总结-序3 1.2 JVM调优总结(一)-- 一些概念 4 1.3 JVM调优总结(二)-一些概念 7 1.4 JVM调优总结(三)-基本垃圾回收算法 9 1.5 JVM调优总结(四)-垃圾回收面临的问题 12 1.6 JVM调优总结(五)-分代...
JVM调优篇
JVM调优工具命令详解
JVM面试资料。 JVM结构:类加载器,执行引擎,本地方法接口,本地内存结构; 四大垃圾回收算法:复制算法、标记-清除算法、标记-整理算法、分代收集算法 ...JVM调优:命令行指令,设置堆内存大小的参数
JVM调优总结PDF,带原理图,延时如何进行分代垃圾回收
分析常见的JVM调优技术及其相关条件,以及在实战中怎么使用到相关的技术进行调优,比较适合中高级开发进行学习,适合对JVM理解较深的开发学习
JVM调优 调优参数 调优方法 , 调优手段 等.
JVM调优总结 JVM调优总结 JVM调优总结
JVM调优攻略.pdf!
JVM调优总结 -Xms -Xmx -Xmn -Xss JVM调优总结 -Xms -Xmx -Xmn -Xss
网上下载的JVM调优实战, 值得一下
NULL 博文链接:https://lihongchao87.iteye.com/blog/1688576
JVM调优实战
java6 JVM 调优参数大全 -XX
JVM调优总结 -Xms -Xmx -Xmn -Xss
深入虚拟机---JVM调优总结(摘自网上网上大牛分享),主要是详细介绍了gc的收集,觉得不错。
系统和JVM调优,深入浅出,简单实用。 配有视频,资料,代码,可以结合视频自己操作。 足够应付面试场景,非常适合面试突击。
JVM调优,GC算法汇总,面试必问,早点准备,很好的资料!
好东西,jvm 调优的详细介绍,让你瞬间明白,打车大图