VMFlags(jvm虚拟机变量设置)

1.JVM 加启动参数 -Xloggc:<file>;如在TOMCAT启动时加此参数;<file>为输出日志名,如 -Xloggc:derek.vgc

export JAVA_HOME="/opt/java"
export CATALINA_OPTS="-Xms512m -Xmx1024m -Xloggc:derek.vgc -Dorg.apache.tapestry.607-patch=true -XX:+HeapDumpOnOutOfMemoryError -XX:-PrintGC"


<Connector port="666" maxHttpHeaderSize="8192"
              maxThreads="1000" minSpareThreads="400" maxSpareThreads="300"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true"  URIEncoding="UTF-8" useBodyEncodingForURI="UTF-8"/>


三、实例,以下给出1G内存环境下java jvm 的参数设置参考:

JAVA_OPTS="-server -Xms800m -Xmx800m  -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true "


●      For AS Java

You need to use the Config Tool to set the JVM parameter:

-XX:+HeapDumpOnOutOfMemoryError

The heap dump is written to:

<j2ee instance>\j2ee\cluster\server<node>\java_pid<pid>.hprof

●      For other applications

Set the following JVM parameter:

-XX:+HeapDumpOnOutOfMemoryError

The heap dump is written to the working directory



VMFlags

When it comes to garbage collector and memory flags VMs from different vendors differ somewhat. Most flags aren't even properly documented by the usage printout of the VM themselves. This page tries to shine some light on what garbage collection related flags there are and what they are good for. It covers several Sun and IBM JVMs
Sun JVMs

Disclaimer: Please note that the data presented in this document has been gathered from several publicly available sources. It is a conscious selection of available VM parameters and even though we tried to check most of the facts presented this document may contain errors.
Choosing a VM

-server
    Instructs the VM to use the server HotSpot VM. This also implies that default heap sizes and permanent generation sizes are different.
    Under 1.5 this option is the default option, if the machine is a server-class machine.
    Supported by: 1.3, 1.4, 1.5
-client
    Instructs the VM to use the client HotSpot VM. This also implies that default heap sizes and permanent generation sizes are different.
    Supported by: 1.3, 1.4, 1.5

Printing Information about GC

-verbose:gc
    Prints out information about garbage collections to standard out. To print the same information to a file, use -Xloggc:<file>
    Example:
    [GC 325407K->83000K(776768K), 0.2300771 secs]
    [GC 325816K->83372K(776768K), 0.2454258 secs]
    [Full GC 267628K->83769K(776768K), 1.8479984 secs]
    See -Xloggc
    Supported by: 1.3, 1.4, 1.5
-Xloggc:<file>
    Prints information about garbage collections to the specified file.
    In conjunction with -XX:+PrintGCDetails this is the best setting for the free GCViewer.
    Supported by: 1.4, 1.5
-XX:+PrintGCDetails
    Instructs the VM to be more verbose when printing out garbage collecion data. Specifically it does not only tell you that there was a collection, but also what impact it had on the different generations.
    This flag is very useful when tuning generation sizes.
    In conjunction with -Xloggc this is the best setting for the free GCViewer.
    Example:
    2.459: [GC 2.459: [DefNew: 3967K->0K(4032K), 0.0141600 secs] 8559K->7454K(16320K), 0.0143588 secs]
    Supported by: 1.4, 1.5
-XX:+PrintGCApplicationStoppedTime
    Instructs the VM to print out the length of actual collection pauses.
    This flag is useful when tuning concurrent collectors.
    Example:
    Total time for which application threads were stopped: 0.0468229 seconds
    Supported by: 1.4, 1.5
-XX:+PrintGCApplicationConcurrentTime
    Instructs the VM to print out the amount of time the applications runs between collection pauses.
    This flag is useful when tuning concurrent collectors.
    Example:
    Application time: 0.5291524 seconds
    Supported by: 1.4, 1.5
-XX:+PrintGCTimeStamps
    Ensures that timestamps relative to the start of the application are printed in the GC log.
    Supported by: 1.4, 1.5
-XX:+PrintTenuringDistribution
    Prints details about the tenuring distribution to standard out. It can be used to show this threshold and the ages of objects in the new generation. It is also useful for observing the lifetime distribution of an application.
    Example:
    5.350: [GC Desired survivor size 32768 bytes, new threshold 1 (max 31)
    - age 1: 57984 bytes, 57984 total
    - age 2: 7552 bytes, 65536 total
    756K->455K(1984K), 0.0097436 secs]
    Supported by: 1.3, 1.4, 1.5

Sizing Heap and Generations

-Xmx<value>
    Overall maximum heap size. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xmx256m sets the maximum heap size to 256mb Supported by: 1.3, 1.4, 1.5
-Xms<value>
    Minimum heap size. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xms256m sets the minimum heap size to 256mb Supported by: 1.3, 1.4, 1.5
-Xmn<value>
    Sets the size of the young generation. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xmn64m sets the young generation size to 64mb Supported by: 1.4, 1.5
-XX:MinHeapFreeRatio=<minimumInPercent>
    Sets the minimal percentage of free heap memory that has to be available after a collection. This parameter can be used to influence when the VM is going to request more memory.
    Example: -XX:MinHeapFreeRatio=70 See -XX:MaxHeapFreeRatio
    Supported by: 1.3, 1.4, 1.5
-XX:MaxHeapFreeRatio=<maximumInPercent>
    Sets the maximal percentage of free heap memory that must at most be available after a collection. This parameter can be used to influence when the VM is going to lower its footprint. In other words it can shrink the heap and therefore memory consumption.
    Example: -XX:MaxHeapFreeRatio=20 See -XX:MinHeapFreeRatio
    Supported by: 1.3, 1.4, 1.5
-XX:NewRatio=<ratio>
    Sets the ratio between young and old generation.
    Example: -XX:NewRatio=3 means that the ratio between the young and old generation is 1:3; in other words, the combined size of eden and the survivor spaces will be one fourth of the heap. See -XX:NewSize and -XX:MaxNewSize
    Supported by: 1.3, 1.4, 1.5
-XX:NewSize=<value>
    Sets minimum size of the young generation.
    Example: -XX:NewSize=64m sets the minimum size of the young generation to 64mb See -XX:NewRatio and -XX:MaxNewSize
    Supported by: 1.3, 1.4, 1.5
-XX:MaxNewSize=<value>
    Sets maximum size of the young generation.
    Example: -XX:NewSize=64m sets the maximum size of the young generation to 64mb See -XX:NewRatio and -XX:NewSize
    Supported by: 1.3, 1.4, 1.5
-XX:SurvivorRatio=<ratio>
    Sets size of the survivor spaces in relation to eden.
    Example: -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6; in other words, each survivor space will be one eighth of the young generation (not one seventh, because there are two survivor spaces). Supported by: 1.3, 1.4, 1.5
-XX:PermSize=<value>
    Sets the initial size of the permanent generation (where classes etc. are stored). This can be useful for application servers using many EJBs and JSPs.
    Example: -XX:PermSize=64m See -XX:MaxPermSize
    Supported by: 1.3, 1.4, 1.5
-XX:MaxPermSize=<value>
    Sets the maximum size of the permanent generation (where classes etc. are stored). This can be useful for application servers using many EJBs and JSPs.
    Example: -XX:MaxPermSize=64m See -XX:PermSize
    Supported by: 1.3, 1.4, 1.5

Choosing and Configuring a Collector

-XX:+UseParallelGC
    Use parallel garbage collection. This collector is also referred to as the throughput collector. It uses a parallel version of the young generation collector. The old (tenured) generation is still cleaned with the default collector.
    Under 1.5 this option is the default option, if the machine is a server-class machine.
    This option can not be used in conjunction with -XX:+UseConcMarkSweepGC.
    Supported by: 1.4.1, 1.5
-XX:+UseParallelOldGC
    Use the parallel old generation collector. Certain phases of an old generation collection can be performed in parallel, speeding up an old generation collection.
    This option automatically enables -XX:+UseParallelGC.
    Supported by: 1.5.0.6
-XX:ParallelGCThreads=<number>
    Specifies the number of threads used in parallel garbage collection when -XX:+UseParallelGC is set. By default a system with N CPUs uses N garbage collection threads.
    Example: -XX:ParallelGCThreads=4 Supported by: 1.4.1, 1.5
-XX:MaxGCPauseMillis=<ms>
    Instructs the VM to try to keep garbage collection pauses shorter than the specified value in ms.
    This option applies in conjunction with -XX:+UseParallelGC and has higher priority than -XX:GCTimeRatio.
    Example: -XX:MaxGCPauseMillis=10 Supported by: 1.5
-XX:GCTimeRatio=<ratio>
    Sets a throughput goal for the VM. The ratio of garbage collection time to application time is 1/(1+<ratio>).
    This option applies in conjunction with -XX:+UseParallelGC and has lower priority than -XX:MaxGCPauseMillis.
    Example: -XX:GCTimeRatio=19 sets a goal of 5% of the total time for garbage collection. Supported by: 1.5
-XX:+UseAdaptiveSizePolicy
    Instructs the VM to keep track of some statistics and resize both the young and the old (tenured) generation based on the collected data.
    This feature is on by default when the option -XX:+UseParallelGC is used.
    Supported by: 1.4.1, 1.5
-XX:+AggressiveHeap
    Instructs the JVM to push memory use to the limit. It inspects the machine resources (size of memory and number of processors) and attempts to set various parameters to be optimal for long-running, memory allocation-intensive jobs. This option is recommended for dedicated server machines.
    The physical memory on the machines must be at least 256MB before AggressiveHeap can be used.
    Beginning with JVM 1.3.1_02 some GC activity is done in parallel.
    Beginning with JVM 1.4 this option implies -XX:+UseParallelGC and -XX:+UseAdaptiveSizePolicy.
    Supported by: 1.3, 1.4, 1.5
-XX:+UseConcMarkSweepGC
    Use concurrent garbage collection. This collector is also referred to as the concurrent low pause collector. It collects garbage in the old (tenured) generation concurrently to executing the application.
    Note that this option can not be used in conjunction with -XX:+UseParallelGC. Instead you may combine it with -XX:+UseParNewGC
    Supported by: 1.4.1, 1.5
-XX:+CMSParallelRemarkEnabled
    If the -XX:+UseParNewGC option is in use the remark pauses may be decreased with the -XX:+CMSParallelRemarkEnabled option.
    Supported by: 1.4.1, 1.5
-XX:+UseParNewGC
    Instructs the VM to use a parallel collector for the young generation. This option should be used in conjunction with -XX:+UseConcMarkSweepGC.
    Supported by: 1.4.1, 1.5
-XX:+UseTrainGC
    Activates the train garbage collector. Note that development for this collector has been stopped since 1.4.2.
    See -Xincgc
    Supported by: 1.3, 1.4, 1.5
-Xincgc
    Activates the incremental (also called train) garbage collector.
    See -XX:+UseTrainGC
    Supported by: 1.3, 1.4, 1.5

Miscellaneous Settings

-Xss<value>
    Sets the size of the stack. In a server system with many threads lowering the stack size may be advantageous to reduce footprint. If the stack is too small, you will start seeing StackOverflowErrors.
    You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xss128k sets the stack size to 128kb Supported by: 1.3, 1.4, 1.5
-XX:+DisableExplicitGC
    Disables calls to java.lang.System.gc().
-XX:SoftRefLRUPolicyMSPerMB=<ms per mb>
    Sets the rate at which the VM clears soft references. The rate is expressed in ms per free mb of heap. For the server VM free heap means potentially free heap using the maximum heap size as set with -Xmx in the calculation. For the client VM the free heap is calculated using the actual current heap size.
    Example: -XX:SoftRefLRUPolicyMSPerMB=1000 instructs the VM to allow softly reachable objects to remain alive for 1s per free mb Supported by: 1.3.1, 1.4, 1.5

Server-Class Machine

Java 5.0 (1.5) defines a class of machines referred to as server-class machines. These are machines that have 2 or more physical processors and 2 or more gb of physical memory. On server-class machines the Sun JVM starts with altered default settings. These are:

-server -XX:+UseParallelGC

Additionally the initial heap size (-Xms) is set to 1/64 of the physical memory, up to 1gb. The maximum heap size (-Xmx) is set to 1/4 of the physical memory, up to 1gb.

Note that on server-class 32bit-Windows systems the VM will nevertheless start with the classic client settings, as most 32bit-Windows Java applications are not server applications.
IBM JVMs

Disclaimer: Please note that the data presented in this document has been gathered from several publicly available sources. It is a conscious selection of available VM parameters and even though we tried to check most of the facts presented this document may contain errors. Also note that the semantics of some of these parameters are different when used with IBM's resettable JVM for the z/OS platform.
Printing Information about GC

-verbose:gc
    Prints out information about garbage collections to standard out.
    See -Xverbosegclog
    Supported by: 1.3.1, 1.4.1, 1.4.2
-Xverbosegclog:<path to file><filename[,X,Y]>
    Prints out information about garbage collections to a file. If the integers X and Y are specified, the output is redirected to X files each containing output from Y GC cycles.
    See -verbose:gc
    Supported by: 1.4.1, 1.4.2

Sizing Heap and Generations

-Xmx<value>
    Overall maximum heap size. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xmx256m sets the maximum heap size to 256mb Supported by: 1.3.1, 1.4.1, 1.4.2
-Xms<value>
    Overall minimum heap size. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xmx256m sets the minimum heap size to 256mb Supported by: 1.3.1, 1.4.1, 1.4.2
-Xinitsh<value>
    Sets the initial size of the system heap. Classes in this heap exist for the lifetime of the JVM. The system heap is never subjected to garbage collection. The maximum size of the system heap is unbounded. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xinitsh256m sets the minimum system heap size to 256mb Supported by: 1.3.1, 1.4.1, 1.4.2
-Xmaxf<value>
    This is a floating point number between 0 and 1, which specifies the maximum percentage of free space in the heap. The default is 0.6, or 60%. When this value is set to 0, heap contraction is a constant activity. With a value of 1, the heap never contracts. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xmaxf0.6 specifies that the heap will be contracted if more then 60% of the heap are unused. Supported by: 1.3.1, 1.4.1, 1.4.2
-Xminf<value>
    This is a floating point number between 0 and 1, which specifies the minimum percentage of free space in the heap. The default is 0.3, or 30%. The heap grows if the free space is below the specified amount. You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xminf0.3 specifies that the heap will be grown if less then 30% of the heap are unused. Supported by: 1.3.1, 1.4.1, 1.4.2

Choosing and Configuring a Collector

-Xgcpolicy:<optthruput|optavgpause|subpool>
    Note that the subpool option was introduced in Version 1.4.1 Service Refresh 1 for AIX only.
    Setting gcpolicy to optthruput disables concurrent mark. If you do not have pause time problems (as seen by erratic application response times or by analysis of the verbose GC output), you should get the best throughput with this option. optthruput is the default setting.
    Setting gcpolicy to optavgpause enables concurrent mark with its default values. If you are having problems with erratic application response times that are caused by normal garbage collections, you can remove those problems at the cost of some throughput when running with the optavgpause option.
    Setting gcpolicy to subpool enables improved object allocation that aims to achieve better performance in allocating objects on the heap. This setting might provide additional throughput optimization because it can improve the efficiency of object allocation and reduce lock contention on large SMP systems. Concurrent mark is disabled when this policy is enabled.
    Supported by: 1.3.1, 1.4.1, 1.4.2
-Xgcthreads<n>
    Sets the total number of threads that are used for garbage collection. On a system with n processors, the default setting is n.
    Supported by: 1.3.1, 1.4.1, 1.4.2
-Xcompactgc
    Compacts the heap every garbage collection cycle. The default is false (that is, the heap is not compacted). This is not recommended.
    Supported by: 1.3.1, 1.4.1, 1.4.2
-Xnocompactgc
    Never compact the heap. Default is false.
    Supported by: 1.3.1, 1.4.1, 1.4.2
-Xnoclassgc
    Disables class garbage collection.
    Supported by: 1.3.1, 1.4.1, 1.4.2

Miscellaneous Settings

-Xss<value>
    Sets maximum native stack size for any thread.
    You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xss128k sets the stack size to 128kb Supported by: 1.3.1, 1.4.1, 1.4.2
-Xoss<value>
    Sets maximum Java stack size for any thread.
    You may use k, m and g for kilobyte, megabyte and gigabyte.
    Example: -Xoss128k sets the stack size to 128kb Supported by: 1.3.1, 1.4.1, 1.4.2
-Xcompactexplicitgc
    Runs full compaction each time java.lang.System.gc() is called. Its default behavior with a java.lang.System.gc() call is to perform a compaction only if an allocation failure triggered a garbage collection since the last java.lang.System.gc() call.
    Supported by: 1.4.1, 1.4.2
-Xnocompactexplicitgc
    Never runs compaction when java.lang.System.gc() is called. Its default behavior with a java.lang.System.gc() call is to perform a compaction only if an allocation failure triggered a garbage collection since the last java.lang.System.gc() call.
    Supported by: 1.4.1, 1.4.2
-Xdisableexplicitgc
    Converts Java application calls to java.lang.System.gc() into no-ops.
    Supported by: 1.4.1, 1.4.2

<think>好的,用户现在想了解如何在Tomcat中设置VM参数。之前他们问了关于Spring MVC的ClassNotFoundException,现在转向Tomcat配置,可能是在部署Spring MVC应用时遇到了性能问题或者需要调整JVM设置。 首先,我需要考虑用户的背景。他们可能是一个正在使用Tomcat部署Java Web应用的开发者,可能遇到了内存不足、垃圾回收问题,或者需要开启调试模式。VM参数的设置对于应用性能调优和问题诊断非常重要,所以用户可能有这些需求。 接下来,用户的需求是什么?他们可能需要知道如何设置VM参数的具体步骤,不同的配置方式(比如在IDE中、在catalina脚本中,或者通过管理界面),以及常见的参数示例。此外,用户可能还不清楚某些参数的具体作用,比如内存设置、GC策略、调试参数等,所以需要给出解释和示例。 然后,我需要考虑不同的设置场景。例如,在IntelliJ或Eclipse这样的IDE中配置Tomcat的VM参数,或者在Linux/Windows服务器上直接修改启动脚本。每种场景下的步骤可能不同,需要分别说明,确保用户能找到适合自己环境的方法。 还要注意常见问题,比如参数格式是否正确,路径是否有空格需要引号包裹,以及如何验证参数是否生效。用户可能在设置后遇到问题,比如参数未生效,这时候需要指导他们如何检查,比如使用jps和jcmd命令。 另外,用户可能需要一些典型的VM参数示例,比如设置堆内存大小、元空间大小,选择垃圾回收器,生成Heap Dump等。这些示例能帮助用户快速应用常见的优化配置。 最后,总结关键点,强调根据应用需求调整参数,以及正确环境下的配置方式。确保用户理解不同设置方法的应用场景,避免混淆。</think>以下是关于Tomcat中设置VM参数(VM Arguments)的详细介绍和操作指南: --- ### **1. 什么是VM Arguments?** VM Arguments是传递给Java虚拟机JVM)的启动参数,用于控制JVM的行为,例如: - 堆内存分配(如初始堆大小、最大堆大小) - 垃圾回收策略 - 调试配置 - 系统属性(如文件编码、时区) --- ### **2. 设置VM Arguments的常见场景** #### **场景1:通过启动脚本配置(推荐)** Tomcat的启动脚本(如`catalina.sh`或`catalina.bat`)中可以直接添加VM参数。 ##### **步骤(Linux/Mac)** 1. 打开`catalina.sh`文件: ```bash vi $CATALINA_HOME/bin/catalina.sh ``` 2. 找到`JAVA_OPTS`的定义位置(通常在文件顶部),添加参数: ```bash JAVA_OPTS="-Xms512m -Xmx1024m -Dfile.encoding=UTF-8" ``` - `-Xms512m`:初始堆内存为512MB - `-Xmx1024m`:最大堆内存为1024MB - `-Dfile.encoding=UTF-8`:设置系统文件编码为UTF-8 ##### **步骤(Windows)** 1. 打开`catalina.bat`文件: ```bat notepad %CATALINA_HOME%\bin\catalina.bat ``` 2. 找到`set JAVA_OPTS=`的行,添加参数: ```bat set JAVA_OPTS=-Xms512m -Xmx1024m -Duser.timezone=GMT+08 ``` --- #### **场景2:通过IDE配置(开发环境)** 在IntelliJ IDEA或Eclipse中运行Tomcat时,可直接在IDE中配置VM参数。 ##### **IntelliJ IDEA** 1. 打开`Run/Debug Configurations`。 2. 选择Tomcat配置,在`VM Options`字段输入参数: ```plaintext -Xmx2048m -XX:+UseG1GC -Dspring.profiles.active=dev ``` ##### **Eclipse** 1. 右键项目 → `Run As` → `Run Configurations`。 2. 在`Arguments`标签页的`VM arguments`框中输入参数。 --- #### **场景3:通过Tomcat管理界面(不推荐生产环境使用)** 在Tomcat的管理界面(如`manager`应用)中,某些版本支持通过`JAVA_OPTS`环境变量设置,但通常更推荐直接修改启动脚本。 --- ### **3. 常用VM参数示例** | **参数** | **作用** | |-------------------------|-------------------------------------------------------------------------| | `-Xms512m` | 初始堆内存大小(如512MB) | | `-Xmx2048m` | 最大堆内存大小(如2GB) | | `-XX:MaxMetaspaceSize=256m` | 设置元空间(Metaspace)最大大小(替代PermGen) | | `-XX:+UseG1GC` | 启用G1垃圾回收器 | | `-Duser.timezone=GMT+08`| 设置JVM时区 | | `-Dspring.profiles.active=prod` | 激活Spring的`prod`配置文件 | | `-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005` | 启用远程调试(端口5005) | --- ### **4. 验证VM参数是否生效** 1. 启动Tomcat后,通过`jps`命令查看Java进程ID: ```bash jps -l ``` 2. 使用`jcmd`查看JVM参数: ```bash jcmd <PID> VM.flags ``` 或通过`jinfo`: ```bash jinfo -flags <PID> ``` --- ### **5. 常见问题** #### **问题1:参数格式错误** - **错误示例**:`-Xmx 1024m`(参数与值之间不能有空格) - **正确格式**:`-Xmx1024m` #### **问题2:路径或值含空格** - 如果路径或参数值包含空格,需用引号包裹: ```bash JAVA_OPTS="-Dconfig.path=/path/with space/" ``` --- ### **总结** - **关键点**:根据应用需求调整内存、GC策略等参数。 - **推荐方式**:在`catalina.sh`/`catalina.bat`中配置,确保环境一致性。 - **生产环境注意**:提前测试参数对性能的影响,避免内存溢出(OOM)或频繁GC。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值