View RSS Feed

Advanced Java

Getting Java Heap Size

Rate this Entry
by , 02-18-2012 at 04:29 PM (863 Views)
In JVM, heap size is considered as a main area. New objects are stored or created here. To get heap size, see this Java code. We will also be considering various memory methods.

Java Code: This is the code to get the heap size in Java
// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();

// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();

Submit "Getting Java Heap Size" to Facebook Submit "Getting Java Heap Size" to Digg Submit "Getting Java Heap Size" to del.icio.us Submit "Getting Java Heap Size" to StumbleUpon Submit "Getting Java Heap Size" to Google

Categories
Memory

Comments