OOM and JVM configuration optimization (1)

Source: Internet
Author: User
OOM is the biggest headache During Java program development: out of memory. In the development process of many developers, such problems are more or less encountered. It is difficult to locate such problems. You often need to judge the code that may cause problems based on experience. There are two main reasons: the object is not released (in many cases, it is often caused by concealed references, leading to hold and cannot be recycled ). The other is that memory is not enough, and JVM heap needs to be added to meet application requirements. Recently, a colleague posted a question about how to solve the OOM problem. I learned that OOM will occur when JVM heap is insufficient and native heap is insufficient, at the same time, JVM heap and native Heap have a relationship of mutual influence and balance, so we carefully read the content about OOM and JVM configuration optimization.

  OOMIn other languages, such as C and Delphi, memory leakage is common and a headache because the memory is allocated and managed by itself. The Java object lifecycle management is implemented by JVM, which simplifies the non-business logic processing of developers. However, this automatic management recycle mechanism is also based on some rules, when these rules are violated, the so-called "Memory Leak" will occur ". OOM (Java heap) Error prompt: Java. Lang. outofmemoryerror. This oom is because the heap memory allocated by the JVM to the application has been exhausted, probably because the application requires a large amount of memory at a high load, therefore, you can modify JVM parameters to add Java heap memory (but you cannot add it without limit. Later OOM may be generated for this reason ). Another scenario is that the application uses objects or resources that are not released, resulting in a sustained increase in memory consumption, this type of problem is often caused by the fact that objects that are no longer needed by the application are referenced by other valid objects, so they cannot be released, may be caused by Business Code logic (exception handling is insufficient, such as I/O resources ), it may also be because you do not know enough about resource release in third-party open-source projects, and the resources are not released after use (such as JDBC's resultset ). Scenarios that are prone to problems: 1. application cache or collection: if the application wants to cache Java objects or store objects in a collection, it is necessary to determine whether a large number of objects will be stored for protection, in order to prevent a large amount of memory from being consumed under the large data volume, and ensure that the cache size does not increase without limit. 2. objects with a long life cycle: try to be brief about the life cycle of objects. Now, the cost of creating and releasing objects is very low, and the optimization is very good, it is better than creating an object for long-term reuse. If you can set the timeout value, try to set the timeout value. 3. Similar to the JDBC connection pool, objects in the pool need to be released and returned after they are used. Otherwise, the pool will increase continuously and the pool will be used in other pools. Pay attention to the release of resources such as resultset and Io. The solution is to find errors or add Java heap memory. There are quite a few tools for detecting such problems, so we will not introduce them here. OOM (native heap) Error prompt: Requested XXXX bytes for chunkpool: Allocate. Out of swap space. Native heap memory is used internally by JVM. This part of memory can be accessed through JNI provided by JDK. This part of memory is very efficient, but management needs to be done by itself, if you are not sure, it is best not to use it to prevent memory leakage. JVM uses native heap memory to optimize code loading (JTI code generation), temporary object space application, and some internal JVM operations. The problem my colleague encountered during the stress test was the OOM, that is, the memory used up. Similarly, oom issues can be divided into two categories: normal use depletion and no resource release depletion. When resources are not released, many of them are not the cause of the programmer. They may be the defects of the referenced third-party packages. For example, many people may encounter Memory leakage problems in the lower version of the Oracle 9 JDBC driver. To identify such problems, we need to observe the growth and usage of native heap memory. After the application of the server, after running for a period of time, the JVM's usage of native heap memory will reach a stable stage. At this time, you can see which operations are frequently performed on native heap memory, which increases native heap memory, I have not found a way to detect native heap memory. Now I can see that the-verbose: JNI parameter is added during JVM startup to observe native heap memory operations. The other case is that native heap memory is consumed normally. The usage of native heap memory mainly depends on JVM code generation, thread creation, and temporary code and object generation for optimization. When native heap memory is used up normally, native heap memory needs to be added, which is in conflict with the increase of Java heap memory we mentioned earlier. The application memory combination is restricted by OS for applications. different operating systems directly affect the allocation of application memory for virtual memory address ranges accessible to processes, 32-bit operating systems generally support a maximum of 4G memory addressing, while Linux generally supports 3G, Windows is 2G. However, these memory sizes are not all used by JVM's Java heap, which is mainly divided into three parts: Java heap, native heap, and the memory occupied by loading resources and class libraries. Therefore, the native heap and Java heap size configurations are mutually restricted. Any part of the allocation may affect the normal operation of the other part. Therefore, if you configure the native heap through the command line, you need to know the Application Usage accurately. Otherwise, the default configuration is used to automatically monitor and optimize the application usage. Note that the virtual memory of the process is different from the actual memory of the machine. For the machine, the actual memory and the virtual memory provided by the hard disk are provided to all processes on the machine, therefore, when setting JVM parameters, its virtual memory should never exceed the actual memory size. To be continued ...... JVM Optimization Configuration

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.