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 - 2: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;

}

2 Jun 2010 - 1:45am by Damjan (not verified)

Chamira: You could use this:
public long occupiedMemory(String filePath){
File file = new File(filePath);
return file.length();
}

28 Oct 2011 - 9:46am by euffgqol (not verified)

aavcrtc

13 Feb 2012 - 8:44pm by Anonymous (not verified)

I need Extera memory for java

12 Apr 2012 - 6:16am by Anonymous (not verified)

testtetsttestsetstsetsesesese

15 May 2012 - 3:07am by Muthuselvi (not verified)

How to get Running code memory in java?

15 May 2012 - 3:08am by Muthuselvi (not verified)

How to get Running program code memory in java?

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.