Getting the Size of the Java Memory Heap

The heap is the area in memory in which objects are created.
// 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();

Comments

26 Jan 2010 - 1:35am by Chamira (not verified)

Is it possible to find how much of memory occupied by a file

public long occupiedMemory(String filePath){

//TODO - Calculate

return occupiedMemoryBytheFile;

}

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.