tomcat jvm memory under linux

There are two common memory overflows:

Java.lang.OutOfMemoryError: PermGen space

java.lang.OutOfMemoryError: Java heap space

 

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

Taking the tomcat environment as an example, other WEB servers such as jboss, weblogic, etc. are the same.


一、java.lang.OutOfMemoryError: PermGen space

The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of ​​memory.
This memory is mainly used by JVM to store Class and Meta information. When Class is loaded by Loader, it will be placed in PermGen space
. The Heap area of ​​(Instance) is different, and GC (Garbage Collection) will not
clean up the PermGen space during the main program runtime, so if your application has a lot of CLASS, it is likely to have a PermGen space error,
which is common in When the web server precompiles the JSP. If your WEB APP uses a large number of third-party jars, the size of which
exceeds the default size of jvm (4M), then this error message will be generated.
Solution: Manually set the MaxPermSize size
Recommendation: Move the same third-party jar file to the tomcat/shared/lib directory, so as to reduce the memory occupied by the jar file repeatedly.

 

2. java.lang.OutOfMemoryError:  java  heap space
The setting of the JVM heap refers to the setting of the memory space that the JVM can allocate and use during the running of the java program. The JVM will automatically set the value of the Heap size when it is started, and
its initial space (ie -Xms) is 1/64 of physical memory , and the maximum space (-Xmx) is 1/4 of physical memory . It can be set by using options such as -Xmn -Xms -Xmx provided by the JVM
. The size of Heap size is the sum of Young Generation and Tenured Generation.
Hint: This exception will be thrown if 98% of the time in the JVM is used for GC and the available Heap size is less than 2%.
Tip: The maximum Heap Size should not exceed 80% of the available physical memory. Generally, the -Xms and -Xmx options should be set to the same, and -Xmn is 1/4 of the -Xmx value. 
Workaround: Manually set the Heap size

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

 

 

 

The meaning of the parameters
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M
-vmargs indicates that the following are the parameters of the VM, so the latter are actually the parameters of the
JVM -Xms128m The heap memory initially allocated by the JVM
-Xmx512m The maximum allowable heap memory allocated by the JVM, allocated on demand
-XX:PermSize=64M The non-heap memory initially allocated by the JVM-
XX:MaxPermSize=128M The maximum allowable non-heap memory allocated by the JVM, allocated on demand

 

 

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

 

 

 

 

Modify the JVM memory size under Linux :

To be added in  catalina.sh under the bin of tomcat , before the location cygwin=false. Note that the quotation marks should be put on, and the red ones are newly added.

# OS specific support. $var _must_ be set to either true or false.
JAVA_OPTS="-Xms256m -Xmx512m  -XX:PermSize=128m -XX:MaxPermSize=256m"
cygwin=false

 

Modify the JVM memory size under windows:

Case 1: The decompressed version of Tomcat must be started through startup.bat to load the configuration

To be added in  catalina.bat under the bin of tomcat

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%" is added later, and the red ones are newly added.

set JAVA_OPTS=-Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true

 

Case 2: There is no catalina.bat under the installed version of Tomcat

The windows service executes bin/ tomcat.exe . It reads the value in the registry, not the settings of catalina.bat.

Modify the registry HKEY_LOCAL_MACHINE/SOFTWARE/Apache Software Foundation/ Tomcat  Service Manager/Tomcat5/Parameters/JavaOptions The
original value is
-Dcatalina.home="C:/ApacheGroup/ Tomcat  5.0"
-Djava.endorsed.dirs="C:/ApacheGroup/ Tomcat  5.0/common/endorsed"
-Xrs

Add -Xms300m -Xmx350m 
to restart the tomcat service, the settings will take effect

 

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

The ratio of each parameter:

The sum of Xmx and PermSize cannot exceed the total memory available to the JVM

PermSize cannot be greater than Xmx

================

How to set Tomcat's JVM virtual machine memory size

The memory used can be set for the Java virtual machine, but if your choice is wrong, the virtual machine will not compensate. You can change the size of the memory used by the virtual machine through the command line. As shown in the table below, there are two parameters used to set the size of the memory used by the virtual machine.
Parameter
description
-Xms
JVM initialization heap size
-Xmx
JVM maximum heap size The sizes of
these two values ​​are generally set as needed. The size of the initialization heap implements the size of the memory that the virtual machine requests from the system at startup. In general, this parameter is not important. However, some applications will rapidly occupy more memory under heavy load. At this time, this parameter is very important. If the memory used when the virtual machine is started is relatively small and there are many objects in this case. Initialization, the virtual machine must repeatedly increase the memory to meet the use. For this reason, we generally set -Xms and -Xmx to be the same size, and the maximum heap size is limited by the physical memory used by the system. Generally, applications that use large amounts of data use persistent objects, and memory usage is likely to grow rapidly. When the memory required by the application exceeds the maximum heap size, the virtual machine will prompt a memory overflow and cause the application service to crash. Therefore, it is generally recommended that the maximum heap size be set to 80% of the maximum available memory.
The default memory that Tomcat can use is 128MB. In larger application projects, this memory is not enough and needs to be increased.
Under Windows, under the file /bin/catalina.bat, under Unix, in front of the file /bin/catalina.sh, add the following settings:
JAVA_OPTS='-Xms [initialized memory size] -Xmx [maximum memory that can be used]'
These two parameters need to be increased. For example:
JAVA_OPTS='-Xms256m -Xmx512m'
means that the initial memory is 256MB, and the maximum memory that can be used is 512MB.
Another thing to consider is the garbage collection mechanism provided by Java. The heap size of a virtual machine determines how much time and how often the virtual machine spends collecting garbage. The acceptable rate of garbage collection is application-dependent and should be adjusted by analyzing the timing and frequency of actual garbage collections. If the heap size is large, full garbage collection will be slow, but less frequently. If you align the heap size with the memory needs, full collections are fast, but more frequent. The purpose of heap sizing is to minimize garbage collection time in order to maximize the processing of client requests within a certain amount of time. When benchmarking , in order to ensure the best performance, the size of the heap should be set large to ensure that garbage collection does not occur during the entire benchmarking process.
If the system spends a lot of time collecting garbage, reduce the heap size. A full garbage collection should take no more than 3-5 seconds. If garbage collection becomes the bottleneck, specify the generation size, examine the detailed output of garbage collection, and study the effect of garbage collection parameters on performance. In general, you should use 80% of physical memory as the heap size. When adding processors, remember to increase memory because allocation can be done in parallel, while garbage collection is not.
Common optimization and configuration
of Tomcat 5 1. JDK memory optimization:
The default memory that Tomcat can use is 128MB, under Windows, in the file {tomcat_home}/bin/catalina.bat, under Unix, in the file {tomcat_home}/bin/catalina.sh Before, add the following settings:
JAVA_OPTS='-Xms[initialized memory size] -Xmx[Maximum memory that can be used]
Generally speaking, you should use 80% of physical memory as the heap size.
2. Connector optimization:
In the configuration in the tomcat configuration file server.xml, the parameters related to the number of connections are:
maxThreads:
Tomcat uses threads to process each request it receives. This value represents the maximum number of threads that Tomcat can create. The default value is 150.
acceptCount:
Specifies the number of requests that can be placed in the processing queue when all available threads for processing requests are used. Requests that exceed this number will not be processed. The default value is 10.
minSpareThreads:
The number of threads created when Tomcat is initialized. The default value is 25.
maxSpareThreads:
Once the number of threads created exceeds this value, Tomcat will close socket threads that are no longer needed. The default value is 75.
enableLookups:
Whether to reverse the domain name lookup, the default value is true. In order to improve processing power, it should be set to false
connnectionTimeout:
network connection timeout, the default value is 60000, unit: milliseconds. Setting it to 0 means that it will never time out, which is a hidden danger. Usually can be set to 30000 milliseconds.
maxKeepAliveRequests: The
number of requests to keep, the default value is 100.
bufferSize:
Input stream buffer size, the default value is 2048 bytes.
compression:
compression transmission, the value is on/off/force, the default value is off.
The parameters related to the maximum number of connections are maxThreads and acceptCount. If you want to increase the number of concurrent connections, you should increase these two parameters at the same time. The maximum number of connections allowed by the web server is also subject to the kernel parameter settings of the operating system, usually about 2000 for Windows and about 1000 for Linux .
3. How to prohibit and allow files in the list directory in tomcat
In {tomcat_home}/conf/web.xml, set the listings parameter to false, as follows:
<servlet>
...
< init-param>
< param- name>listings</param-name>
<param-value>false</param-value>
< /init-param>
...
</servlet>
4. How to prohibit and allow a host or IP address to access
<Host name in tomcat ="localhost" ...>
...
< Valve className="org.apache.catalina.valves.RemoteHostValve"
allow="*.mycompany.com,www.yourcompany.com"/>
< Valve className="org. apache.catalina.valves.RemoteAddrValve"
deny="192.168.1.*"/>
...
</Host>
Server configuration
JAVA_OPTS='-server -Xms512m -Xmx768m -XX:NewSize=128m -XX:MaxNewSize=192m -XX:SurvivorRatio=8'

 

Source: http://blog.csdn.net/yougou_sully/article/details/6457570

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326282606&siteId=291194637