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.
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
posted
0
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.
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...
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
posted
0
Hi Jim, This is a very good idea and i will surely try it thanks for replying Best wishes Saurabh