Lack of memory in Java even the computer having memories available

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

Author: Maniero, 2019-03-28

3 answers

I imagine you are getting out of memory error. This is not to say that all memory was occupied, but that it had no way of allocating. Even with GC there is some fragmentation and there are some patterns that garbage collector uses that may need to allocate something in a certain way that is not possible, even if for what it would need to be.

In some cases one party may be exhausted even if it has room in others. The heap is not an area unique. There are even cases that the problem may be in unmanaged memory that may report something abnormal.

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

So out of memory error does not mean that all memory was consumed, just that it was not possible to allocate what was requested, for various reasons.

 3
Author: Maniero, 2019-05-31 16:46:33

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

 2
Author: Wallace Henrique Silva, 2019-03-29 12:53:35

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 meta data and others (Classes that have been allocated, fields/methods information, constant...). 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 the JVM from the memory used by your program. If 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 for minimize waste of resources and optimize the life cycle of objects being used.

 0
Author: Diego da Silva Pinheiro, 2019-05-22 13:35:10