Lack of Java memory even though the computer has available memories

Asked

Viewed 412 times

1

The Java Virtual Machine "JVM" can run out of memory even if the physical machine has available memory?

  • It can. When you give for example a OutOfMemoryError in a Java program is because the JVM could not allocate memory, even though more memory is available on the host machine. There are boot settings to tell JVM how much memory it will be able to allocate in total.

  • Internally the JVM is divided into several memory areas and the size of each can be configured. And because these areas have size limits of their own, they can burst, even if the server has more memory available.

3 answers

3


I imagine you are getting memory loss error. This is not to say that all memory has been occupied but rather that you had no way to make allocation. Even with GC there is some fragmentation and there are some patterns that the Garbage Collector uses that may need to allocate something in a certain way that is not possible, although for what it would need would be.

In some cases one part may suffer exhaustion even if it has space in others. The heap is not a single area. There are even cases that the problem may be in unmanaged memory that can report something abnormal.

In addition you may be asking to allocate a very large object, for it has no space, but still has plenty of space available for other slightly smaller objects.

So lack of memory error does not mean that all memory was consumed, only that it was not possible to allocate what was requested, for several reasons.

2

Yes, the JVM does not use all the available memory on the machine, it is initially limited and may cause errors such as Outofmemory. To solve this type of problem, you can, when starting the application, set the memory that will be used by JVM by passing parameters like -Xms, -Xmx, among others.

0

The JVM works with a memory segmentation mentality, where there are very specific subdivisions each aimed at a certain purpose.

  • Heap Memory: Used to store the objects you allocate. Here you can configure them using the flags -Xms (Initial size) and -Xmx (Maximum size)
  • Non-heap Memory: Used for metadata and others (Classes that have been allocated, field/method information, constants...). Also configurable by flag "-XX:Maxpermsize"
  • And the memory allocated to the JVM itself, after all it must exist somewhere :^)

Here it is necessary to distinguish the memory allocated for JVM of memory used by your program. Case, the memory used tends to want to pass the memory allocated, this will generate an exception (The famous Outofmemory). This is where your programming strategy will come in to minimize resource waste and optimize the lifecycle of the objects being used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.