This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java Memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Memory" Watch "Java Memory" New topic

Java Memory

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, can someone explain how the java memory works, below is what I've got:


I added an array of 44,703,332 strings to program, and I got a 'heap space error'
Using 44,703,331 gives the TOTAL the same as the MAX.
So thats quite clear.

But my free memory is still 75M, intuitively I would have thought FREE+TOTAL = MAX
so in my example above, you would expect the FREE to be 0, but its 75M instead.

So its not as simple as I thought, can anyone explain how the 3 values relate to each other.

Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Runtime.freeMemeory() returns an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.
Runtime.totalMemory() returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment.
Runtime.maxMemory() is proabaly the only 'fixed' value.

As I see it, these values correlate somewhat loosly and there is no formula for it that would work all the time (if ever).

I no good English.
Mukesh Ranjan

Joined: Jun 24, 2009
Posts: 22
Which exception you are getting exxactly,

Check if it OutOfMemory Exception or StackOverflowException,

If you are doing it inside some thread or if it is a local variable then try to look for stack size, rather than heap size..

For more detail you can look at site:
I agree. Here's the link:
subject: Java Memory
Similar Threads
Garbage Collection Help!
Help regarding
Free memory: Total memory: Max memory:
memory impact on statement
Size of a empty class