> 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();
> > 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.