Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to java process if the physical memory is very low on system

I have a Java process running doing some tasks, after a couple of hours there are multiple other applications opened on the system causing a very low physical memory available on the system.

So, if the system has no physical memory/very less memory left, how would my java process respond to such a situation? Would it throw a 'out of memory' exception?

enter image description here

like image 474
Sudeep Avatar asked Feb 14 '19 14:02

Sudeep


People also ask

What happens when Java process runs out of memory?

OutOfMemoryError is a runtime error in Java which occurs when the Java Virtual Machine (JVM) is unable to allocate an object due to insufficient space in the Java heap. The Java Garbage Collector (GC) cannot free up the space required for a new object, which causes a java. lang.

Why does Java need so much memory?

Java is also a very high-level Object-Oriented programming language (OOP) which means that while the application code itself is much easier to maintain, the objects that are instantiated will use that much more memory.

What happens when host runs out of memory?

An ESXi host can run out of memory if virtual machines consume all reservable memory in a memory overcommitted environment. Although the powered on virtual machines are not affected, a new virtual machine might fail to power on due to lack of memory.

What happens when JVM runs out of memory which exception is thrown?

If the JVM is not able to allocate memory for the newly created objects an exception named OutOfMemoryError is thrown. This usually occurs when we are not closing objects for long time or, trying to act huge amount of data at once.


1 Answers

When RAM is exhausted the OS will usually use swap or pagefile to provide virtual memory:

RAM is a limited resource, whereas for most practical purposes, virtual memory is unlimited. There can be many processes, and each process has its own 2 GB of private virtual address space. When the memory being used by all the existing processes exceeds the available RAM, the operating system moves pages (4-KB pieces) of one or more virtual address spaces to the computer’s hard disk. This frees that RAM frame for other uses. In Windows systems, these “paged out” pages are stored in one or more files (Pagefile.sys files) in the root of a partition.

Paging usually results in a severe performance penalty because even modern SSD storage is not as fast as RAM. If the memory scarcity continues, the system may start thrashing.

Assuming that the JVM does not require more memory, e.g. it is already constrained by -Xmx and allocated all allowed memory, it will continue to run. Usually when the memory is exhausted OS will not allow new processes to start e.g. attempting to start new JVM process will result in following error:

Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine. 

At the end of the day it depends on the OS configuration. Usually this situation is not something you want to spend time investigating because RAM is much cheaper than developer's time.

like image 130
Karol Dowbecki Avatar answered Oct 01 '22 22:10

Karol Dowbecki