This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Runtime methods

 
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 14497
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan is correct. freeMemory() is the amount that could be allocated right now without running the garbage collector.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic