Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / October 2003

Tip: Looking for answers? Try searching our database.

Remaining Memory

Thread view: 
Bill Dennis - 09 Sep 2003 22:35 GMT
I want to preempt getting an "Out of Memory" error.  Can I calculate
the amount of memory remaining using the following formula?

long memoryRemaining = runTime.maxMemory() - runTime.totalMemory() +
runTime.freeMemory();

Thanks.

Bill Dennis
Steve W. Jackson - 10 Sep 2003 21:16 GMT
>:I want to preempt getting an "Out of Memory" error.  Can I calculate
>:the amount of memory remaining using the following formula?
[quoted text clipped - 5 lines]
>:
>:Bill Dennis

That will give you a pretty good idea what the free memory is at any
given instant.  But you may want to look into why you're getting the out
of memory trouble to begin with.  It's not always a simple matter to
free up memory.  Simply calling System.gc() will not guarantee that any
objects are in fact collected, for instance.  Before throwing that
error, the JVM will make several attempts to free sufficient memory for
the allocation it's attempting before giving up.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Bill Dennis - 11 Sep 2003 04:56 GMT
Thanks, Steve.  I know why the out of memory is happening.  The
application is a Print Preview function, and the requirement was to
see the entire report preview all at once (i.e. not broken up into
pages, but as a continuous scroll), with no delay as the user scrolled
through the report.  I don't have disk space to buffer the report
into, so I keep it all in memory.  Normally, there's no problem.  But
some users have been creating large reports, with thousands of rows of
multi-column data.  That's what causing the memory problem that I want
to catch.

Bill Dennis
Diego Cattelan - 30 Sep 2003 17:19 GMT
You can catch memory errors with

try{
   //allocate memory
}catch(OutOfMemoryError oome){
   //free memory
   //display error
}
> I want to preempt getting an "Out of Memory" error.  Can I calculate
> the amount of memory remaining using the following formula?
[quoted text clipped - 5 lines]
>
> Bill Dennis
Stephen Thomas - 01 Oct 2003 18:27 GMT
> I want to preempt getting an "Out of Memory" error.  Can I calculate the
> amount of memory remaining using the following formula?
>
> long memoryRemaining = runTime.maxMemory() - runTime.totalMemory() +
> runTime.freeMemory();

In theory, yes.  In practise, the result may not be useful.  For
some JVMs obtaining those quantities (particularly runTime.freeMemory())
may be a far less trivial operation than it seems.  In a highly concurrent
application, any result you get may be completely irrelevent by the
time you get to act on it, as memory may be being churned while the
value is being computed.

As has been pointed out elsewhere, System.gc() is not guaranteed to
do anything useful (although it should really strive to do so :-) ).
If the JVM's GC is concurrent or incremental (I know at least one is,
because I implemented it) then calling System.gc() may merely schedule
a GC cycle to proceed ASAP, without the calling thread actually waiting
for it.  Even if the thread does wait, there is no guarantee that any
other thread will be similarly blocked.  In the case of a genuine OOM,
the JVM will probably try several GC cycles, amongst other things,
before finally giving up.

Stephen Thomas
Glen Herrmannsfeldt - 01 Oct 2003 20:16 GMT
> > I want to preempt getting an "Out of Memory" error.  Can I calculate the
> > amount of memory remaining using the following formula?
[quoted text clipped - 8 lines]
> time you get to act on it, as memory may be being churned while the
> value is being computed.

(snip more about System.gc())

Also, what about fragmentation?   Do most, or any, actually move allocated
regions around?   So even if enough was available it might not be available
for one allocation.

-- glen
Stephen Thomas - 02 Oct 2003 11:08 GMT
> Also, what about fragmentation?   Do most, or any, actually move allocated
> regions around?   So even if enough was available it might not be
> available for one allocation.

All JVM's intended for serious use must have a way of dealing with
fragmentation - too many real-world apps would die in very short order
if they didn't.  How they deal with fragmentation may differ from JVM
to JVM, though.  That said, you can almost certainly assume that any
JVM, when faced with an OOM, will have attempted a defragmenting GC
cycle before giving up, although probably not as a first resort if a
less disruptive alternative is available.  Again, System.gc() may or
may not force a defragmenting cycle, depending on your JVM (or, in
some cases, different configurations of a particular JVM).

Stephen Thomas
Roedy Green - 02 Oct 2003 18:57 GMT
>Also, what about fragmentation?

The early Javas used a simple mark sweep garbage collection.  Although
they used handles, they never actually moved objects for compaction.
The current ones use generational collectors which segregate
long-lived and temporary objects. They must have the ability to move
objects around for defragging.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.