• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting memory information? *urgent*

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I get the initial Java heap size and the maximum Java heap size from within java code? (The values given to the JVM at startup)
And how do I get the amount of free memory in the computer? (Runtime.freeMemory() only gives the JVMs currently available memory, but it can ask the system for more so this value isn't interesting).
The reasons for these questions is that I have to implement a cacheing function on a server and then needs to know how much memory the cache should be allowed to use.
I hope someone has an answer!
/Andreas
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Lang Package
RunTime class
method we have 2 methods
1. freeMemory()
which returns amout ao free memory avalible for JVM.
2. totalMemory()
which returns total memory allocated for JVM.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andreas,
Nooooooooo...
If you're implementing a caching routine, you shouldn't have to worry about the memory. Okay, you should worry, but you shouldn't have to handle it. What you should do is use the Object Reference API that is available in JDK1.2.
Read this article: http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/index.html.
It will give you a good idea how to implement a caching routine.
-Peter
 
Andreas Johansson
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vani:
In Lang Package
RunTime class
method we have 2 methods
1. freeMemory()
which returns amout ao free memory avalible for JVM.

2. totalMemory()
which returns total memory allocated for JVM.


These two methods only gives info about what the JVM currently has, not about what it can get.
If the JVM only has 16Mb allocated but the system has 128Mb free memory then I won't know about the free memory before I try to allocate it. If the system have more memory to give to the JVM, then everything is fine, otherwise I will get an OutOfMemoryError and that is what I want to avoid.
/Andreas
 
Andreas Johansson
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Tran:

If you're implementing a caching routine, you shouldn't have to worry about the memory. Okay, you should worry, but you shouldn't have to handle it. What you should do is use the Object Reference API that is available in JDK1.2.
Read this article: http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/index.html.
It will give you a good idea how to implement a caching routine.
-Peter


Hi!
I have previously read that article and tried using SoftReferences.
Since the data can be quite large (10Mb up to 140Mb) I don't want to clear the cache if there is any possibillity to keep it. The data are in different languages (if each language takes up 40Mb, two of them takes up 80Mb in the cache etc.) Therefore the cache usually have a few (2-10) large items.
The problems that I encountered where the following:
When I tried this with the SoftReferences it seemed that they where cleared almost directly when the JVM needed more memory. What I understand is that when the JVM gets low on memory, it first run the gc, and the gc is totally free to choose if it wants to reclaim the SoftReferences. First after that it asks the OS for more memory (if still needed). But I want to use all (or about 90%) of the free memory for the cache.
I also wanted to control so that the most used languages stays in the cache if possible.
Regards,
Andreas
reply
    Bookmark Topic Watch Topic
  • New Topic