Handle Java's "Cannot allocate memory" error, cannotallocate

Source: Internet
Author: User

Handle Java's "Cannot allocate memory" error, cannotallocate

Today, when I configured the DCA server, I suddenly encountered a Cannot allocate memory error when I checked the java version.

[Root @ elcid-prod1 ~] # Java-version

Java HotSpot (TM) 64-Bit Server VM warning: INFO: OS: commit_memory (0x00007ff55c5ea000, 4096, 0) failed; error = 'could not allocate memory '(errno = 12)

#

# There is insufficient memory for the Java Runtime Environment to continue.

# Native memory allocation (mmap) failed to map 4096 bytes for committing reserved memory.

# An error report file with more information is saved:

#/Root/hs_err_pid5138.log

After looking at the relevant documents, I found that the error is actually as it said, and there is no way to allocate memory.

 

The problem is inherent with the way Java allocates memory when executing processes. when Java executes a process, it must fork () then exec (). forking creates a child process by duplicating the current process. by duplicating the current process, the new child process will request approximately the same amount of memory as its parent process, essential tially doubling the memory required. however, this does not mean all the memory allocated will be used, as exec () is immediately called to execute the different code within the child process, freeing up this memory. as an example, when Stash tries to locate git, the Stash JVM process must be forked, approximately doubling the memory required by Stash.

 

There are two solutions. The first one is to use another method (such as posix_spawn) to replace the fork/exec method of Java to apply for memory, and the second is to enable Over-commit of the system, skip the available memory check and direct allocation.

 

The first method is troublesome. You need to use a custom JNI binding to change the underlying Java call, so do not try it first.

The second method is relatively simple, but may cause errors in programs dependent on the C language malloc.

 

In short, try the second one first. After all, it is about changing the system variables.

Temporary changes: echo 1>/proc/sys/vm/overcommit_memory

Permanent change: edit/etc/sysctl. conf, modify vm. overcommit_memory = 1, restart the server, or log on again.

 

Reference: http://bryanmarty.com/2012/01/14/forking-jvm/

Related Article

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.