`
jayjayjaylun
  • 浏览: 90002 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Common Errors in Setting Java Heap Size

    博客分类:
  • java
阅读更多
Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mistakes I have seen when using them:

    * Missing m, M, g or G at the end (they are case insensitive). For example,

      java -Xmx128 BigApp
      java.lang.OutOfMemoryError: Java heap space

      The correct command should be: java -Xmx128m BigApp. To be precise, -Xmx128 is a valid setting for very small apps, like HelloWorld. But in real life, I guess you really mean -Xmx128m

    * Extra space in JVM options, or incorrectly use =. For example,

      java -Xmx 128m BigApp
      Invalid maximum heap size: -Xmx
      Could not create the Java virtual machine.

      java -Xmx=512m HelloWorld
      Invalid maximum heap size: -Xmx=512m
      Could not create the Java virtual machine.

      The correct command should be java -Xmx128m BigApp, with no whitespace nor =. -X options are different than -Dkey=value system properties, where = is used.

    * Only setting -Xms JVM option and its value is greater than the default maximum heap size, which is 64m. The default minimum heap size seems to be 0. For example,

      java -Xms128m BigApp
      Error occurred during initialization of VM
      Incompatible initial and maximum heap sizes specified

      The correct command should be java -Xms128m -Xmx128m BigApp. It’s a good idea to set the minimum and maximum heap size to the same value. In any case, don’t let the minimum heap size exceed the maximum heap size.

    * Heap size is larger than your computer’s physical memory. For example,

      java -Xmx2g BigApp
      Error occurred during initialization of VM
      Could not reserve enough space for object heap
      Could not create the Java virtual machine.

      The fix is to make it lower than the physical memory: java -Xmx1g BigApp

    * Incorrectly use mb as the unit, where m or M should be used instead.

      java -Xms256mb -Xmx256mb BigApp
      Invalid initial heap size: -Xms256mb
      Could not create the Java virtual machine.

    * The heap size is larger than JVM thinks you would ever need. For example,

      java -Xmx256g BigApp
      Invalid maximum heap size: -Xmx256g
      The specified size exceeds the maximum representable size.
      Could not create the Java virtual machine.

      The fix is to lower it to a reasonable value: java -Xmx256m BigApp

    * The value is not expressed in whole number. For example,

      java -Xmx0.9g BigApp
      Invalid maximum heap size: -Xmx0.9g
      Could not create the Java virtual machine.

      The correct command should be java -Xmx928m BigApp

NOTE:

How to set java heap size in Tomcat?
Stop Tomcat server, set environment variable CATALINA_OPTS, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh or catalina.bat for how this variable is used. For example,

set CATALINA_OPTS=”-Xms512m -Xmx512m”  (Windows)
export CATALINA_OPTS=”-Xms512m -Xmx512m”  (ksh/bash)
setenv CATALINA_OPTS “-Xms512m -Xmx512m”  (tcsh/csh)

In catalina.bat or catallina.sh, you may have noticed CATALINA_OPTS, JAVA_OPTS, or both can be used to specify Tomcat JVM options. What is the difference between CATALINA_OPTS and JAVA_OPTS? The name CATALINA_OPTS is specific for Tomcat servlet container, whereas JAVA_OPTS may be used by other java applications (e.g., JBoss). Since environment variables are shared by all applications, we don’t want Tomcat to inadvertently pick up the JVM options intended for other apps. I prefer to use CATALINA_OPTS.

How to set java heap size in JBoss?
Stop JBoss server, edit $JBOSS_HOME/bin/run.conf, and then restart JBoss server. You can change the line with JAVA_OPTS to something like:

JAVA_OPTS=”-server -Xms128m -Xmx128m”

How to set java heap size in Eclipse?
You have 2 options:
1. Edit eclipse-home/eclipse.ini to be something like the following and restart Eclipse.

-vmargs
-Xms64m
-Xmx256m

2. Or, you can just run eclipse command with additional options at the very end. Anything after -vmargs will be treated as JVM options and passed directly to the JVM. JVM options specified in the command line this way will always override those in eclipse.ini. For example,

eclipse -vmargs -Xms64m -Xmx256m

How to set java heap size in NetBeans?
Exit NetBeans, edit the file netbeans-install/etc/netbeans.conf. For example,

netbeans_default_options=”-J-Xms512m -J-Xmx512m -J-XX:PermSize=32m -J-XX:MaxPermSize=128m -J-Xverify:none

How to set java heap size in Apache Ant?
Set environment variable ANT_OPTS. Look at the file $ANT_HOME/bin/ant or %ANT_HOME%\bin\ant.bat, for how this variable is used by Ant runtime.

set ANT_OPTS=”-Xms512m -Xmx512m”  (Windows)
export ANT_OPTS=”-Xms512m -Xmx512m”  (ksh/bash)
setenv ANT_OPTS “-Xms512m -Xmx512m”  (tcsh/csh)

How to set java heap size in jEdit?
jEdit is a java application, and basically you need to set minimum/maximum heap size JVM options when you run java command. jEdit by default runs with a default maximum heap size 64m. When you work on large files, you are likely to get these errors:

java.lang.OutOfMemoryError: Java heap space
at java.lang.String.concat(String.java:2001)
at org.gjt.sp.jedit.buffer.UndoManager.contentInserted(UndoManager.java:160)
at org.gjt.sp.jedit.Buffer.insert(Buffer.java:1139)
at org.gjt.sp.jedit.textarea.JEditTextArea.setSelectedText(JEditTextArea.java:2052)
at org.gjt.sp.jedit.textarea.JEditTextArea.setSelectedText(JEditTextArea.java:2028)
at org.gjt.sp.jedit.Registers.paste(Registers.java:263)

How to fix it? If you click a desktop icon, or Start menu item to start jEdit: right-click the icon or menu item, view its property, and you can see its target is something like:

C:\jdk6\bin\javaw.exe -jar “C:\jedit\jedit.jar”

You can change that line to:

C:\jdk6\bin\javaw.exe -Xmx128m -Xms128m -jar “C:\jedit\jedit.jar”

If you run a script to start jEdit: just add these JVM options to the java line inside the script file:

java -Xmx128m -Xms128m -jar jedit.jar

If you start jEdit by running java command: just add these JVM options to your java command:

java -Xmx128m -Xms128m -jar jedit.jar

Note that when you run java with -jar option, anything after -jar jar-file will be treated as application arguments. So you should always put JVM options before -jar. Otherwise, you will get error:

C:\jedit>java -jar jedit.jar -Xmx128m
Unknown option: -Xmx128m
Usage: jedit [] []

How to set java heap size in JavaEE SDK/J2EE SDK/Glassfish/Sun Java System Application Server?
Stop the application server, edit
$GLASSFISH_HOME/domains/domain1/config/domain.xml, search for XML element name java-config and jvm-options. For example,

<java-config suffix=”…”>
<jvm-options>-Xmx512m</jvm-options>
<jvm-options>-XX:NewRatio=2</jvm-options>
<jvm-options>-XX:MaxPermSize=128m</jvm-options>
…</java-config>

You can also change these settings in the web-based admin console, typically at http://localhost:4848/, or https://localhost:4848/. Go to Application Server near the top of the left panel, and then on the right panel, click JVM Settings -> JVM Options, and you will see a list of existing JVM options. You can add new ones and modify existing ones there.

Yet another option is to use its Command Line Interface (CLI) tool command, such as:

./asadmin help create-jvm-options
./asadmin help delete-jvm-options

They may be a bit hard to use manually, but are well suited for automated scripts. 


昨天查了一天的资料,运气不错,收获不小,解决了一个老大难问题:JBoss内存紧张的问题。
这是一个困扰了我两年整的问题,就是从前年这个时候,用JBoss 3.2.1架站以来,始终是一个大问题。不大的站点,1G的内存都不够用,经常要消耗500Mb的交换内存(swap)。
原来是自己犯了非常低级的错误,不懂JAVA_OPTS各参数的含义造成的。
之前的JAVA_OPTS是 -Xms 520m -Xmx 1220m -Xss 15120k +XX:AggressiveHeap
这个JAVA_OPTS犯了2个致命的错误:
1. +XX:AggressiveHeap会使得 Xms 1220m没有意义。这个参数让jvm忽略Xmx参数,疯狂地吃完一个G物理内存,再吃尽一个G的swap。
另外Xmx作为允许jvm使用的最大内存数量,不应该超过物理内存的90%。
而之所以使用了这个参数,是因为不加的话,JBoss会在运行一天左右的时间后迅速崩溃,上机课是,甚至出现过半个小时就崩溃的情况。
之所以要用这个参数,用swap支持服务器运行,是因为犯了下面的错误:
2. -Xss 15120k
这使得JBoss每增加一个线程(thread)就会立即消耗15M内存,而最佳值应该是128K,默认值好像是512k.
这就是JBoss刚启动时,还有200Mb内存富余,但会在一个小时内迅速用完,因为服务器的threads在迅速增加。前3天,每天都多吃80Mb左右的swap.在第四天开始稳定下来。今年春节在外度假,观察到了这个现象,却不理解其原因:服务器在线程到达100之后,一般不再增加新的线程,新增加的在用完之后,会被迅速destroy,?褂玫哪诖嬉不厥樟恕R虼耍旁诖航谄诩洌鄄斓椒衿髟?1.25-2.10所使用的线程基本是 1.21-1.23创建的,因此没有再消耗新的内存。服务器持续运行时间,也因此大大超乎我5天的预期,到达了20天。
昨天所作的修改:
1.修改JAVA_OPTS,去掉+XX:AggressiveHeap,修改Xss。现在的JAVA_OPTS为:
-Xms 520m -Xmx 900m -Xss 128k
2.修改deploy/jbossweb-tomcat55.sar/service.xml
将maxThreads根据目前的访问量由默认的250降为75,并使用jboss 4默认未写在标准service.xml里面而jboss 3写入了的2个参数: maxSparseThreads=55,minSparseThreads=25
3.修改了oracle-ds.xml将最大连接数有150降为50.
4.去掉了一些不用的服务。 


安装Java开发软件时,默认安装包含两个文件夹,一个JDK(Java开发工具箱),一个JRE(Java运行环境,内含JVM),其中JDK内另含一个JRE。如果只是运行Java程序,则JRE已足够;而JDK则只有开发人员才用到。这里将为大家介绍设置JVM内存分配的几招。
一、设置JVM内存设置

1. 设置JVM内存的参数有四个:

-Xmx    Java Heap最大值,默认值为物理内存的1/4,最佳设值应该视物理内存大小及计算机内其他内存开销而定;

-Xms    Java Heap初始值,Server端JVM最好将-Xms和-Xmx设为相同值,开发测试机JVM可以保留默认值;

-Xmn    Java Heap Young区大小,不熟悉最好保留默认值;

-Xss    每个线程的Stack大小,不熟悉最好保留默认值;

2. 如何设置JVM内存分配:

(1)当在命令提示符下启动并使用JVM时(只对当前运行的类Test生效):

java -Xmx128m -Xms64m -Xmn32m -Xss16m Test

(2)当在集成开发环境下(如eclipse)启动并使用JVM时:

a. 在eclipse根目录下打开eclipse.ini,默认内容为(这里设置的是运行当前开发工具的JVM内存分配):

-vmargs  -Xms40m  -Xmx256m -vmargs表示以下为虚拟机设置参数,可修改其中的参数值,也可添加-Xmn,-Xss,另外,eclipse.ini内还可以设置非堆内存,如:-XX:PermSize=56m,-XX:MaxPermSize=128m。

此处设置的参数值可以通过以下配置在开发工具的状态栏显示:

在eclipse根目录下创建文件options,文件内容为:org.eclipse.ui/perf/showHeapStatus=true

修改eclipse根目录下的eclipse.ini文件,在开头处添加如下内容:


-debug  options  -vm  javaw.exe 重新启动eclipse,就可以看到下方状态条多了JVM信息。

b. 打开eclipse-窗口-首选项-Java-已安装的JRE(对在当前开发环境中运行的java程序皆生效)

编辑当前使用的JRE,在缺省VM参数中输入:-Xmx128m -Xms64m -Xmn32m -Xss16m

c. 打开eclipse-运行-运行-Java应用程序(只对所设置的java类生效)

选定需设置内存分配的类-自变量,在VM自变量中输入:-Xmx128m -Xms64m -Xmn32m -Xss16m

注:如果在同一开发环境中同时进行了b和c设置,则b设置生效,c设置无效,如:

开发环境的设置为:-Xmx256m,而类Test的设置为:-Xmx128m -Xms64m,则运行Test时生效的设置为:

-Xmx256m -Xms64m

(3)当在服务器环境下(如Tomcat)启动并使用JVM时(对当前服务器环境下所以Java程序生效):

a. 设置环境变量:

变量名:CATALINA_OPTS

变量值:-Xmx128m -Xms64m -Xmn32m -Xss16m

b. 打开Tomcat根目录下的bin文件夹,编辑catalina.bat,将其中的%CATALINA_OPTS%(共有四处)替换为:-Xmx128m -Xms64m -Xmn32m -Xss16m

二、查看设置JVM内存信息

Runtime.getRuntime().maxMemory();  //最大可用内存,对应-Xmx

Runtime.getRuntime().freeMemory();  //当前JVM空闲内存

Runtime.getRuntime().totalMemory();  //当前JVM占用的内存总数,其值相当于当前JVM已使用的内存及freeMemory()的总和

关于maxMemory(),freeMemory()和totalMemory():

maxMemory()为JVM的最大可用内存,可通过-Xmx设置,默认值为物理内存的1/4,设值不能高于计算机物理内存;

totalMemory()为当前JVM占用的内存总数,其值相当于当前JVM已使用的内存及freeMemory()的总和,会随着JVM使用内存的增加而增加;

freeMemory()为当前JVM空闲内存,因为JVM只有在需要内存时才占用物理内存使用,所以freeMemory()的值一般情况下都很小,而JVM实际可用内存并不等于freeMemory(),而应该等于 maxMemory()-totalMemory()+freeMemory()。及其设置JVM内存分配。 

分享到:
评论
1 楼 明天的昨天 2010-11-03  
文章不错,转载了。谢谢

相关推荐

    Common Errors English

    If you want to know how... 1. Improve Your Punctuation & Grammar.... How to improve your performance in coursework and examinations Write with Confidence Solutions and examples for everyday writing needs

    common-debug.rar_Common Errors

    "common-debug.rar_Common Errors"这个压缩包文件显然与通用错误调试有关,而"common_errors"标签进一步强化了这一主题。文件列表中包含"common-debug.c"和"common-debug.h"两个文件,这通常表明我们正在处理C语言...

    PDF电子书《Compile and Runtime Errors in Java》

    《PDF电子书《Compile and Runtime Errors in Java》》是一本由Mordechai (Moti) Ben-Ari编写的深入探讨Java编程语言中编译时错误和运行时错误的专业书籍。作者是魏茨曼科学研究所科学教学系的成员,该书在2007年1月...

    C-P-P-Common-Errors.rar_Common Errors

    本资源"Common Errors"是一个针对C++编程中的常见错误进行汇集的文档,旨在帮助读者更好地理解和解决这些问题。下面将详细讨论一些C++编程中常见的错误类型及其解决方案。 1. **语法错误**:这是编程中最直观的错误...

    C-99-common-errors.zip_Common Errors

    "C-99-common-errors.zip" 包含了一个名为 "C 99个常见错误.pdf" 的文件,该文件详细阐述了99个C语言初学者可能会遇到的问题。以下是对这些常见错误的概述: 1. **语法错误**:这是最常见的错误类型,包括拼写错误...

    kafka处理超大消息的配置 org.apache.kafka.common.errors.RecordTooLargeException

    java.lang.RuntimeException: java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.RecordTooLargeException: The request included a message larger than the max message size the server ...

    Bugs as deviant behavior__a general approach to inferring errors in systems code

    标题与描述中的“Bugs as deviant behavior__a general approach to inferring errors in systems code”这一主题探讨了一种新颖的方法,旨在自动识别系统代码中的错误。这种方法的核心在于将程序中的bug视为偏离...

    Learn Java the Easy Way: A Hands-On Introduction to Programming

    Table of Contents Chapter 1: Getting Started Chapter 2: Build a Hi-Lo Guessing Game App! Chapter 3: Creating a GUI for Our Guessing Game ...Appendix: Debugging and Avoiding Common Errors in Java

    org.tmatesoft.svn_1.9.1.eclipse.zip

    no libapr-1 in java.library.path no libapriconv-1 in java.library.path C:\Program Files (x86)\Intel\iCLS Client\libeay32.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform C:\Program Files ...

    前端开源库-common-errors

    "前端开源库-common-errors" 就是一个专注于处理前端开发过程中常见错误的库,它包含了一系列错误处理类和实用的工具函数。这个库旨在帮助开发者更好地识别、预防和解决在开发过程中遇到的问题,提升代码质量和稳定...

    Upper-Bound Errors in Far-Field Antenna Parameters Determined From Planar Near-Fileld Measurements

    NBS Technical Note 667是关于近场误差测量的误差分析的基础性文章。是Yaghjian1975年在美国NBS工作期间的技术报告。

    On the postprocessing removal of correlated errors in GRACE temporal gravity field solutions

    (Geophys Res Lett 34:&lt;BR&gt;L13302, 2007), for reducing the correlated errors in the Stokes coefficients (SCs) of the spherical harmonic expansion&lt;BR&gt;of the GRACE determined monthly geopotential ...

    英文原版-Errors in Veterinary Anesthesia 1st Edition

    Errors in Veterinary Anesthesia is the first book to offer a candid examination of what can go wrong when anesthetizing veterinary patients and to discuss how we can learn from mistakes. ,解压密码 ...

    java7帮助文档

    see Appendix D: Disabling Cryptographic Algorithms in Java PKI Programmer's Guide and Disabled Cryptographic Algorithms in Java Secure Socket Extension (JSSE) Reference Guide. Various enhancements ...

    common-java-errows.rar_error

    "common-java-errows.rar_error"这个压缩包文件显然包含了一个名为"common java errows.doc"的文档,旨在列举并解释一些常见的Java编程错误,以便帮助初学者理解和解决这些问题。下面,我们将详细讨论一些可能涵盖的...

    Java Performance Companion(Addison,2016)

    The authors, who are all leading Java performance and Java HotSpot VM experts, help you improve performance by using modern software engineering practices, avoiding common mistakes, and applying tips ...

    Beginning Cryptography With Java 带源码

    and the JSSE for encryption and authentication The ways in which padding mechanisms work in ciphers and how to spot and fix typical errors An understanding of how authentication mechanisms are ...

    Avoiding Read While Write Errors When In-Software Flash

    Avoiding Read While Write Errors When In-Software Flash

Global site tag (gtag.js) - Google Analytics