jvm memory configuration

-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 allowed by the JVM Allocated heap memory, allocated on demand
-XX:PermSize=64M JVM initially allocated non-heap memory
-XX:MaxPermSize=128M JVM maximum allowable allocated non-heap memory, allocated on demand




-Xms and -Xmx are an option of Java commands , which is used to set the available memory size when your application starts and the available memory size at runtime.
Xmx is an option of java, used to set the maximum amount of memory your application can use (good, just your application, not the entire jvm), if your program takes a lot of memory, then you need to modify the missing Provincial settings, such as when configuring tomcat, if the traffic and programs are very large, you need to increase this value, but there is one thing to remember, do not exceed the memory of your machine, then your machine I can't take it anymore, and I'll be dead.
Xms is another parameter for setting memory. It is used to set the size of the memory stack when the program is initialized. If you increase this value, the startup performance of your program will be improved. However, there are also the previous restrictions, as well as restrictions by Xmx. 
Although the implementations of different virtual machines vary widely, their operating modes are the same, but the performance is different.
A virtual machine is just a software implementation, it is a machine in memory, and jre is installed on our machine to generate this jvm. Generally speaking, a jvm is generated every time an application is run, but there can be multiple programs in the same jvm.

1. 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 allowed by the JVM Allocated heap memory, allocated on demand
-XX:PermSize=64M JVM initially allocated non-heap memory
-XX:MaxPermSize=128M JVM maximum allowable allocated non-heap memory, allocated on demand
We first understand the mechanism of JVM memory management, and then Explain what each parameter represents.

1) heap (Heap) and non-heap (Non-heap) memory

According to the official statement: "The Java virtual machine has a heap, the heap is the runtime data area, and the memory of all class instances and arrays is allocated from here. The heap is in Created when the Java virtual machine starts." "The memory outside the heap in the JVM is called non-heap memory (Non-heap memory)".
It can be seen that the JVM mainly manages two types of memory: heap and non-heap. In short, the heap is the memory accessible to Java code, which is reserved for developers; non-heap is reserved for the JVM,
so the memory required for method area, JVM internal processing or optimization (such as JIT compiled code) caches), each class structure (such as runtime constant pool, field and method data), and the code for methods and constructors are in off-heap memory.

Heap memory allocation

The heap memory initially allocated by the JVM is specified by -Xms, and the default is 1/64 of the physical memory; the maximum heap memory allocated by the JVM is specified by -Xmx, and the default is 1/4 of the physical memory. When the default free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of -Xmx;
When the free heap memory is greater than 70%, the JVM will reduce the heap up to the minimum limit of -Xms. Therefore, the server generally sets -Xms, -Xmx equal to avoid adjusting the size of the heap after each GC.
Note: If -Xmx is not specified or the specified value is too small, the application may cause a java.lang.OutOfMemory error. This error comes from the JVM and is not Throwable and cannot be caught by try...catch.

Non-heap memory allocation

The JVM uses -XX:PermSize to set the initial value of non-heap memory, the default is 1/64 of the physical memory; XX:MaxPermSize sets the maximum non-heap memory size, the default is 1/4 of the physical memory. (One more thing: The default value of MaxPermSize is related to the -server -client option
. The default MaxPermSize under the -server option is 64m, and the default MaxPermSize under the -client option is 32m. I have no experiment with this.)
The full name of the PermGen space in the above error message It is Permanent Generation space, which refers to the permanent storage area of ​​memory. Haven't figured out if PermGen space belongs to off-heap or just off-heap, but at least it does.
If XX:MaxPermSize is set too small, it will cause java.lang.OutOfMemoryError: PermGen space is out of memory.
Talk about why the memory is beneficial:
(1) This part of the memory is used to store the information of Class and Meta. When the Class is loaded, it is put into the PermGen space area, which is different from the Heap area where the Instance is stored.
(2) GC (Garbage Collection) will not clean up the PermGen space during the running of the main program, so if your APP loads a lot of CLASS, it is likely to have a PermGen space error.
  This kind of error is common when the web server precompiles the JSP. 

2) JVM memory limit (maximum value)

First, the JVM memory is limited to the actual maximum physical memory. Assuming that the physical memory is infinite, the maximum JVM memory has a great relationship with the operating system. To put it simply, although the controllable memory space of a 32-bit processor has 4GB, the specific operating system will impose a limit,
which is generally 2GB-3GB (generally 1.5G-2G under Windows system and 1.5G-2G under Linux system). 2G-3G), and processors above 64bit will not have restrictions.
2. Why can Eclipse start after I set both -Xmx and -XX:MaxPermSize to 512M on some machines, but not on some machines?
Through the above introduction to JVM memory management, we have learned that JVM memory includes two types: heap memory and non-heap memory. In addition, the maximum memory of JVM first depends on the actual physical memory and operating system. Therefore, setting VM parameters causes the program to fail to start for the following reasons:
1) The value of -Xms in the parameter is greater than -Xmx, or the value of -XX:PermSize is greater than -XX:MaxPermSize;
2) The value of -Xmx and -XX The sum of :MaxPermSize exceeds the maximum limit of JVM memory, such as the current operating system maximum memory limit, or actual physical memory, etc. When it comes to actual physical memory, it should be noted that
if your memory is 1024MB, it is not possible to use 1024MB in the actual system, because part of it is occupied by hardware.
3. Why does Eclipse not execute the corresponding settings when writing the above parameters to the eclipse.ini file?
So why the same parameter is valid in shortcut or command line but not in eclipse.ini file? This is because we did not follow the setting rules of the eclipse.ini file: the
parameter is in the form of "item value". If there is a space in the middle, it needs to be written in a new line. If there is a space in the value, it needs to be enclosed in double quotation marks. For example, we use the -vm C:/Java/jre1.6.0/bin/javaw.exe parameter to set the virtual machine, and
write this in the eclipse.ini file:
-vm
C:/Java/jre1.6.0/bin/javaw.exe
-vmargs
-Xms128M
-Xmx512M
-XX:PermSize=64M
-XX:MaxPermSize=128M
The actual running result can be viewed through the "Configuration Details" button in the "Help" - "About Eclipse SDK" window in Eclipse.
It should also be noted that the content of the eclipse.ini file that comes with the Eclipse compressed package is as follows:
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
-vmargs
-Xms40m
-Xmx256m
Among them –launcher.XXMaxPermSize (note the two connecting lines at the front) and the meaning of the -XX:MaxPermSize parameter are basically the same, I think the only difference is that the former is the parameter set when eclipse.exe starts,
and the latter is eclipse Arguments in the JVM used. In fact, it is enough to set one of the two, so here you can comment out –launcher.XXMaxPermSize and the next line with #.
4. Other startup parameters. If you have a dual-core CPU, maybe try this parameter:
-XX:+UseParallelGC
to make GC run faster. (Just a new parameter added to GC in JDK 5)
Supplement:
  If your WEB APP uses a large number of third-party jars, the size of which exceeds the default size of the server jvm, then there will be a memory problem.
Solution: To set the MaxPermSize size,
you can select the corresponding server such as tomcat5 in myelipse, expand the JDK sub-item page inside, to increase the JVM parameter settings for server startup:
-Xms128m
-Xmx256m
-XX:PermSize=128M
-XX:MaxNewSize=256m
-XX:MaxPermSize=256m
or manually set the MaxPermSize size, such as tomcat,
modify TOMCAT_HOME/bin/catalina.bat, add the following line above echo "Using CATALINA_BASE: $CATALINA_BASE":
JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128mSuggestion
: Move the same third-party jar file to the tomcat/shared/lib directory, which can reduce the repeated memory usage of the jar file

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941251&siteId=291194637