Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Memory Control

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic