Die-knock Memory 2 How the--java process suddenly hangs out

Source: Internet
Author: User

The JVM is running out of memory to cause the process to die. Native memory allocation (MMAP) failed to map

Deploying many Java processes on a single server is already the norm for microservices, but there are some pits. Background, some Java processes on the test server suddenly hang up, and the log of the call back is found as follows:
# there is insufficient memory for the Java Runtime Environment to continue. # Native Memory allocation (MMAP) failed to map 1786867712 bytes for committing reserved memory. # Possible reasons: # The system is out of physical RAM or swap space # in + bit mode, the process size limit was hit # P Ossible Solutions: # Reduce Memory load on the system # Increase physical memory or swap space # Check if swap backing Sto Re is full # Use + bit Java on a + bit OS # decrease Java Heap Size (-XMX/-XMS) # Decrease number of Java threads # DECR Ease Java Thread stack sizes (-XSS) # Set larger code cache with-xx:reservedcodecachesize= # This output file could be Trun cated or incomplete. # # Out of Memory Error (os_linux.cpp:2673), pid=28610, tid=139813184919296 # # JRE Version:java (TM) SE Runtime Environme NT (8.0_40-B26) (Build 1.8.0_40-b26) # Java Vm:java HotSpot (TM) 64-bit Server VM (25.40-b25 mixed Mode linux-amd64 compre ssed oops) # 

  

is obviously due to insufficient memory on the server

There's not enough memory in two cases.

Speculation: There is not enough memory in two casesspeculation 1, the Java program uses a memory request that is out of the JVM's allocation, and this class only throws the Java.lang.OutOfMemoryError:Java heap Space,java process is not affected, and the next time you can continue to accept requests for servicespeculation 2, when the JVM tries to request a piece of memory out of the system's available (RSS) memory like the operating system, Linux ends the Java process and throws an error like this. This scenario is also common: for example, in a 16G memory server running two services, two services start allocating 10G of memory at the same time, because the JVM garbage collection mechanism is generally not a problem, the normal service, when two services need to use more memory than 16G, Will be killed by Linux for a service. (Note: When the JVM starts, memory allocated through parameters such as-XMX will only affectVIRT, see the previous blog, "size relationship between Java process and Linux memory"confirmed:

To prove that it's the most important thing, it's all my speculations.

A Linux server with 1G of memory

 Free shared buffers Cached Mem:       984 119 864 0 2  -/+         886  Swap:      20441999 

Around 3G in physical memory +swap (886+1999)

To join the Java program, because it is to observe the Java memory beyond whether it will terminate the process of Java, so the idea is as follows design 1, with a thread to apply for allocating memory 2, the main thread keeps reporting the status of survival

The procedure is as follows

 Packageorg.hejinbin.memory.test;Importjava.io.IOException;/*** used to demonstrate that insufficient memory causes the Java process to exit, note: The size entered does not result in exceeding int (1024x768 * size) *@authorHe Jinbin QQ 277803242 (bun) 2016.12.29*/ Public classTest { Public Static voidMain (string[] args)throwsIOException, interruptedexception {NewThread (NewRunnable () { Public voidrun () {Try {                    //the size of the received test memory, in units m                    byte[] ByteSize =New byte[50]; System.out.print ("Input want to allocation memory:"); inttemp =System.in.read (bytesize); String Sizestr=NewString (bytesize, 0, temp); intSize =Integer.parseint (Sizestr.trim ()); System.out.println (1024 * 1024 *size); byte[] bt =New byte[1024 * 1024 *size]; System.out.println ("SUCC. Allocation: "+ size +" M "); //30-point resistance to prevent garbage collectionThread.Sleep (1000 * 60 * 30); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace ();        }}). Start ();  while(true) {            //report 1 minutes later .Thread.Sleep (1000 * 60); System.out.println (Thread.CurrentThread (). GetId ()+ ": I ' m alive"); }    }}

Confirmed speculation 1:

Run the program, enter 1000 (this will throw an exception if the memory exceeds the age)

Output alive, which proves 1, that using memory beyond will not cause the Java process to die

Confirmed speculation 2:

Open windows 1, run, enter [email protected]:/home/hejinbin# java-xmx3048m-xms3048m org.hejinbin.memory.test.Test

Input want to allocation memory:

Open windows 2, run, enter [email protected]:/home/hejinbin# java-xmx3048m-xms3048m org.hejinbin.memory.test.Test input want to Alloc ation Memory: Then enter 1500 in Window 1 to run as follows: input want to allocation memory:1500
Succ. allocation:1500m then in Window 2, enter 1500input want to allocation memory:1500
Succ. allocation:1500m now found Windows 1 Java process was killed, complete as follows [email protected]:/home/hejinbin# java-xmx3048m-xms3048m Org.hejinbin.memory.test.Test
Input want to allocation memory:1500
Succ. allocation:1500m
Killed

Confirmed the speculation. 2

Conclusion: Today's micro-services are popular (take advantage of the heat). Many processes run on a single server at the same time, it must be noted that the memory allocated to Java only and must be within the server's available memory. Otherwise, it's possible that a process was suddenly killed by Linux.

Finally there is a thought, why my process was killed, and did not produce hs_err_pid*.log log it?

Die-knock Memory 2 How the--java process suddenly hangs out

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.