`
happyjeef18
  • 浏览: 14318 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jvm调优

    博客分类:
  • java
阅读更多

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!

----------------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics