confused about Runtime.freeMemory()

T

Thomas Kellerer

Hello,

according to the JavaDocs Runtime.freeMemory() should report the size of the
free memory, but is this not affected by the -Xmx parameter. When I run the
following program:

public class Memory
{
public static void main(String[] args)
{
System.out.println(Runtime.getRuntime().freeMemory());
}
}

it prints the same amount regardless of the -Xmx parameter value:

java Memory --> 1850616
java -Xmx512m Memory --> 1850616

It seems always to report something around 2-4 MB (either JDK 1.5 or JDK 1.6)

When I use maxMemory() - totalMemory() this does seem to reflect the current
heap size a lot closer.

What am I missing here?

I would like to implement a threshold where certain information is no longer
stored internally when the memory gets low. But with freeMemory() this does not
seem to be a viable solution.

Thanks in advance
Thomas
 
M

Mark Rafn

Thomas Kellerer said:
according to the JavaDocs Runtime.freeMemory() should report the size of the
free memory

Free JVM memory. This is memory the JVM has allocated, but is not currently
using. To get free system memory, you need to use system-specific tools,
probably through JNI.
but is this not affected by the -Xmx parameter.

It will never be bigger than -Xmx.
System.out.println(Runtime.getRuntime().freeMemory());
it prints the same amount regardless of the -Xmx parameter value:
java Memory --> 1850616
java -Xmx512m Memory --> 1850616

Right. Since the VM never gets to the max, the max amount doesn't matter.
Try it with -Xms512m -Xmx512m. Then the VM will always allocate half a gig,
regardless of what it thinks it needs.
I would like to implement a threshold where certain information is no longer
stored internally when the memory gets low. But with freeMemory() this does
not seem to be a viable solution.

SoftReference is the normal way to approach this need. This lets the VM
decide when it needs more memory. There are specific cases where you want
more control, and then you usually want something more system-specific (JNI or
reading /proc information) to really tell what's going on outside the VM.
 
M

Mike Schilling

Thomas Kellerer said:
Hello,

according to the JavaDocs Runtime.freeMemory() should report the size of
the free memory, but is this not affected by the -Xmx parameter. When I
run the following program:

public class Memory
{
public static void main(String[] args)
{
System.out.println(Runtime.getRuntime().freeMemory());
}
}

it prints the same amount regardless of the -Xmx parameter value:

java Memory --> 1850616
java -Xmx512m Memory --> 1850616

It seems always to report something around 2-4 MB (either JDK 1.5 or JDK
1.6)

When I use maxMemory() - totalMemory() this does seem to reflect the
current heap size a lot closer.

What am I missing here?

That -Xmx sets the maximum size of the heap, not its current size, and
freeMemory() reports how much is currently free. maxMemory() -
totalMemory() tells you how much the heap can expand. If what you want to
know is "How much more could possibly fit in the heap?", I think that would
be

maxMemory() - (totalMemory() - freeMemory())
 
T

Thomas Kellerer

That -Xmx sets the maximum size of the heap, not its current size, and
freeMemory() reports how much is currently free.
Hmm. My understanding of "currently free" was (until now) the amount of
memory that can be allocated. But it seems I was wrong

maxMemory() - totalMemory() tells you how much the heap can expand. If what you want to
know is "How much more could possibly fit in the heap?", I think that would
be

maxMemory() - (totalMemory() - freeMemory())
Yes, that's exactly what I wanted to know. I think the JavaDocs are a
bit too short regarding this topic.

Thanks to all for the answers

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top