This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Runtime methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime methods" Watch "Runtime methods" New topic
Author

Runtime methods

adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Hi,

Runtime class provides api methods like
1)maxMemory()
2)totalMemory() and
3)freeMemory()

I was under the impression that :
(1)maxMemory() is the max memory which JVM can work upon (say 128 MB)
(2)totalMemory() is the available memory with JVM (say 100MB)
(3)freeMemory() is the memory which JVM can work upon if it needs to allocate memory to objects,etc. (say 28MB).

I was guessing that
maxMemory = totalMemory + freeMemory.

but it isn't so !!

1)Can anyone explain why the above equation isn't true ?How do we figure out the relation between these (we need to have some calculation in place so that we know how much memory we are using or how much memory we will require more) ?

2)How do we decide the max memory we can allocate to the JVM ?


Following is the code i wrote to check that.





Cheers,
Adithya.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
I'm not exactly sure, but my guess is that totalMemory() is the amount of memory that is available to the JVM right now and maxMemory is the maximum amount of memory that the JVM will request from the OS.

The JVM doesn't immediately request all the memory it can get. It will request some, use that memory (which affects freeMemory()) and when it needs more free memory, but there aren't any objects to be garbage collected, it will request more memory from the OS, adding this to totalMemory(). It will keep doing this until it hits maxMemory(), at this point it will throw an OutOfMemoryError.

Again, this is only my best guess.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Stephan is correct. freeMemory() is the amount that could be allocated right now without running the garbage collector.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime methods
 
Similar Threads
Info on Current JVM Memory usage
Getting memory information? *urgent*
java heap memory check
memory setting
How to check how much memory is used by a program??