File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to calculate total memory after which i will be getting out-of memory error??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to calculate total memory after which i will be getting out-of memory error???" Watch "how to calculate total memory after which i will be getting out-of memory error???" New topic
Author

how to calculate total memory after which i will be getting out-of memory error???

Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Hi !
Is there a way to find out the total amount of memory which the jvm can have during run time. what i mean is that can i find out how much memory the user has allocated by setting -mx value for the jvm. Runtime.getTotalMemory() gives me the value that is currently allocated to the jvm and freememory() gives me the amount of memory free for creating the object.So is there a way to find out the total memory after which i will be getting an out-of-memory error, may be some workaround or so.
Thanx in advance.
Saurabh
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, one way to find out is to force the issue:
<pre>
public static long getAbsoluteTotalMemory() {
byte[] reserve = new byte[10000];
List list = new LinkedList();
try {
for ( ; ; )
list.add(new byte[100000]);
}
catch (OutOfMemoryError e) {
reserve = null;
list.clear();
System.gc();
return Runtime.getRuntime().totalMemory();
}
}</pre>
Normally you aren't supposed to catch an Error, but it works in this case because the first thing we do is make more memory available. The "reserve" is probably unnecessary - it's there in case the LinkedList clear() method tries to do anything - even the smallest thing - which would attempt to allocate more memory before freeing everything up, as intended. Unlikely perhaps - but without knowing the internals of LinkedList, it seems like a nice idea to be safe here.
A disadvantage to this method is that once you call it, the JVM will] be allocated the max memory available - I don't know if there's any way to decrease this amount subsequently. So other applications running on the same box may be slowed down more than otherwise necessary.

[This message has been edited by Jim Yingst (edited December 16, 2001).]


"I'm not back." - Bill Harding, Twister
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Thanx Jim,
This was really a nice stuff !!
i have solved my problem.
but for knowledge sake can you please tell if the user having only 64 MB of RAM and starting the application setting -mx250m then will i get the correct amount of memory at which the application will generate the out-of-memory error or not. i tried it and it gives me a windows generated message that "your system is low on virtual memory".
thanx for your efforts.
Saurabh
Chirag Shah
Greenhorn

Joined: May 02, 2001
Posts: 9
Saurabh,
OutOfMemory exception generally pops up if a particular object holds more memory than approx-40-50 mb. I bumped into them when the jvm was running at a lower overall memory consumption. Try using -Xmx to increase the heap size.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No, increasing the heap size in this case will just make the system even more unhappy. The message "your system is low on virtual memory" means there is not enough memory available outside the JVM. If you've only got 64 MB RAM, and you're trying to allocate 250 MB, then you're going to be using the disk a lot for virtual memory - which of course is much slower than actual RAM, but if you need to do it, you can. If you're running low on virtual memory, the two basic choices are: (a) use less, by killing other processes and/or allocating less memory to your JVM in the first place (250 MB is a lot for your system), or (b) allocate more disk space to virtual memory. On Windows NT go to Control Panel -> System -> Performance -> Virtual Memory -> Change. Other systems should have similar options.
Of course, the other option is to buy more memory and install it. This is highly recommended if we're talking about just one machine, or a small number of machines that you control - but it may not be an option in other cases.
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
That's right Jim !
now that's the problem with your code since the method you sent will try to allocate that much amount of memory to the jvm and will result that window's message, so the message comes before even starting of my application and the user will be shocked to see that, however in normal case that won't be a problem until jvm reaches that far. what do you think ???
Moreover if i look at the memory usage by my application on the Task Manager then it shows me that total amount of memory which i specified in -mx option to be allocated. Can i release that extra amount of memory?? i know that making a call to Garbage collector will not make it run and neither it makes memory available to the system, if it runs on it's own.
Saurabh
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yeah, this is pretty much what I said in my first post. I don't know of a way to release this memory back to the operating system, other than terminating the JVM. Garbage collection has nothing to do with it really - that just free up memory in the heap, making it available for the JVM to do something else with it. It does nothing to help other applications outside the JVM, as far as I know. It's possible that a JVM could be constructed which might return no-longer-needed memory to the OS, but I haven't heard of it - and there's certainly no standard interface to do it with.
Look at it this way though - if memory usage is going to be a problem, would you rather find out about this when you first start the prgram, or later when you're midway through a complex operation and the program suddenly dies without saving anything? I'd usually prefer the former. The user (or install package) needs to configure the -mx option to match what the environment can really handle. If it's set incorrectly, or if the user's machine just isn't big enough to handle the program it's trying to run, there's only so much you can do. Use less, or get more. Good luck...
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Yeah Jim !
That's absolutely correct, i agree with your point of making all the calculations beforehand i.e before starting of the real application. i have made some changes in your code and now it is looking like this.
public long getAbsoluteTotalMemory()
{
byte[] reserve;
LinkedList list=new LinkedList();;
try
{
reserve = new byte[10000]
for ( ; ; )
{
list.add(new byte[10000]);
}
}
catch (OutOfMemoryError e)
{
reserve = null;
list.clear();
list=null;
System.gc();
return Runtime.getRuntime().totalMemory();
}
}
Surprisingly, this is working very fine without even allocating
-mx memory to the java process, if i look into the Task Manager, as against the previous code.
Saurabh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to calculate total memory after which i will be getting out-of memory error???