aspose file tools
The moose likes Java in General and the fly likes memory available to jvm at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "memory available to jvm at run time" Watch "memory available to jvm at run time" New topic
Author

memory available to jvm at run time

Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Can anybody help me how to check the memory available to jvm at run time. i have to give the user a message that your system is going out of memory and log him out successfully before java itself gives out-of-memory error.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Runtime.totalMemory();
Runtime.freeMemory();
[This message has been edited by Cindy Glass (edited September 07, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Hi cindy !
thanks for your reply i tried this but if the user increases the size of the java process by setting -mx then how will i know what value has he set for the process . Is there any way i can get the values assigned to -ms -mx while running the java process.Also if he doesn't set the -ms -mx values then how can i get the default maximum memmory size allocated to the java process.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I don't know a way to do this. What I suggest - when your program starts up, create a new byte[10000] and save it in a static variable somewhere. This is your reserve memory. Then put a try / catch around your main method (or wherever is most convenient) which will catch an OutOfMemoryException. In the catch, set the static variable to null and call System.gc(). This should free up the memory previously held by the byte[10000], and you now should have enough memory to keep your system alive for a little while at least - enough to log out, hopefully. You can experiment with the size of the byte[] array to see how much you really need here. Good luck...


"I'm not back." - Bill Harding, Twister
shashank bapat
Ranch Hand

Joined: Jul 11, 2001
Posts: 69
Originally posted by Jim Yingst:
I don't know a way to do this. What I suggest - when your program starts up, create a new byte[10000] and save it in a static variable somewhere. This is your reserve memory. Then put a try / catch around your main method (or wherever is most convenient) which will catch an OutOfMemoryException. In the catch, set the static variable to null and call System.gc(). This should free up the memory previously held by the byte[10000], and you now should have enough memory to keep your system alive for a little while at least - enough to log out, hopefully. You can experiment with the size of the byte[] array to see how much you really need here. Good luck...

Jim
Nice Trick. Have u ever tried this ??
I had written an application which used to reduce the number of processing threads based on memory left ..
And I dont see any problem with earlier suggested answer of using totalMemory() freeMemory() methods.
Trick is call them repeatedly ... if ur program runs for a long time I assume it loops somewhere ... unless its unusual type of program or u r unusual type of programmer ... So just check once in a loop and decide at that point ... because if I remember correctly when JVM increases its memory footprint totalMemory() updates its return value ... so I do not see how initial jvm options come into play (unless u call totalMemory() only once in the begining)
Thanks
-Shashank

-Shashank<BR>MS(CS),BS(CS),SCJP2
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Hi Jim,
This is a very good idea and i will surely try it
thanks for replying
Best wishes
Saurabh
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: memory available to jvm at run time
 
Similar Threads
NullPointerException is not getting garbage collected properly in Multi threaded environment
JVM memory auto configuration
Assigning/allocating memory issues
Heap memory usage
Dynamically determining the amount of available memory