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/