JVM memory limit and adjustment

JVM memory limit and adjustment

 

Today, using java -jar to execute a jar file indicates that the memory is not enough and the heap size of the virtual machine needs to be set. The following are reference materials:

Heap and 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 created when the Java virtual machine starts." "The memory outside the heap in the JVM is called non-heap memory." It can be seen that JVM mainly manages two types of memory: heap and non-heap. Simply put, the heap is the memory accessible by the Java code, which is reserved for developers to use; the non-heap is reserved for their own use by the JVM, so the method area, the internal processing of the JVM or the memory required for optimization (such as JIT compiled code Cache), each class structure (such as runtime constant pool, field and method data), and the code of methods and construction methods are all in non-heap memory. 

Heap memory allocation The
  initial allocated memory of the  JVM is specified by -Xms, and the default is 1/64 of the physical memory; the maximum 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 until the minimum limit of -Xms. Therefore, the server generally sets -Xms and -Xmx equal to avoid adjusting the heap size after each GC. 
Non-heap memory allocation 
  JVM uses -XX:PermSize to set the initial value of non-heap memory, the default is 1/64 of physical memory; XX:MaxPermSize sets the maximum non-heap memory size, the default is 1/4 of physical memory. 
JVM memory limit (maximum) 
  First of all, the JVM memory is limited to the actual maximum physical memory (nonsense! Haha), assuming that the physical memory is infinite, the maximum JVM memory has a great relationship with the operating system. Simply put, although the 32-bit processor has 4GB of controllable memory space, the specific operating system will give a limit, this limit is generally 2GB-3GB (generally 1.5G-2G under Windows system, and 1.5G-2G under Linux system 2G-3G), and there will be no restrictions on processors above 64bit. 


  Usually, in order to avoid memory overflow and other problems, we need to set the environment variables 
  JAVA_OPTS -Xms256M -Xmx512M, etc. [For the server, generally set to the same] 
  but sometimes this setting may not work (for example, when the Server application When loading more classes, that is, when the jvm loads classes, the objects in the permanent domain increase sharply, so that the jvm constantly adjusts the permanent domain size. In order to avoid adjustment), you can use more parameter configurations, 
  such as: java -Xms512m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=128m 
  Among them, use the -XX:MaxPerSize flag to increase the size of the permanent domain, and the -XX:PerSize flag to set the initial value 


reference:
http://wallimn.javaeye.com/blog/505610

Reprinted at: https://my.oschina.net/acmfly/blog/113256

Guess you like

Origin blog.csdn.net/yucaifu1989/article/details/108107124