This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Memory Control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory Control" Watch "Memory Control" New topic
Author

Memory Control

Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi. I have a multi-user system in which one of the potential operations that a user can perform uses a large amount of memory. Hypothetically, if many users were to run this operation at once, the system could run out of memory. Is there a way for the JVM to "check" if it has enough memory to perform a specified operation and then wait until that amount of memory is available or do I need to develop a queueing system of some sort?

Thanks.
Jeff Storey
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The Runtime class has several methods you can use here. freeMemory() tells you how much memory is currently available, and maxMemory() - totalMemory() tells you how much additional memory the JVM process is allowed to use (based on the -Xmx setting used when invoking Java). You need all this info to know how much memory is really available. But even then, you don't know how much memory could be freed up by garbage collection. And unfortunately there's no good general way to know in advance how much memory a particular operation will require. For some methods it's possible to estimate fairly accurately, e.g. if the method requires temporary use of a very large byte array, you can estimate based on the size of the array. But for many other methods it would be much harder to estimate the memory usage.


"I'm not back." - Bill Harding, Twister
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Unfortunately, you seem to be confirming what I had already believed. The short answer I guess is that there is no way to do this, especially since the large operation method is being called out of an API and not something I wrote (although I do have the source code for it). Thanks for the quick reply.

Jeff
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can try the operation, catch an out of memory exception and tell the user "I didn't have enough free memory to do that. Try again?" We don't see this kind of thing often, because if we run out of memory allocating a short string things are probably pretty darned bad. But if you don't have enough memory to allocate a multi-megabyte thing it might be worth trying again later.

If users could stand the wait (is slow better than failing?) maybe you could synchronize the operation in a static method or on a global object so only one user runs at a time.

Hey, you had queuing in the OP now that I go back to read. I like that. Something based on an adjustable pool or count.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Control