Actually, there is no good answer, since the amount of heap memory that the JVM has does not coincide with how free the operating system is in the heap, and it is not the same as the amount of heap memory that can be assigned to your application.
This is because the JVM and OS heaps are different. When the JVM runs out of memory, it can start garbage collection, defragment its heap, or request more memory from the OS. Since unused garbage-free objects still exist, but are technically โfreeโ, they make the concept of free memory a bit fuzzy.
In addition, heap memory fragments; how / when / if memory defragmentation depends on the implementation of the JVM / OS. For example, an OS heap may have 100 MB of free memory, but due to fragmentation, the largest available contiguous space may be 2 MB. Thus, if the JVM requests 3 MB, it may receive an error due to memory, although 100 MB is still available. The JVM cannot know in advance that the OS will not be able to allocate this 3 MB.
BlueRaja - Danny Pflughoeft
source share