Forums Register Login

Memory Control

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1002 times.
Similar Threads
how to calculate total memory after which i will be getting out-of memory error???
re: Stack, Heap - no help from intermediate group
memory
max no of files in solaris
JBoss Memory Management
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:22:59.