T
thomas_okken
I'm working on a rather memory-hungry application. There's not much I
can do to make the app use less memory, but I'm hoping that at least I
can find a way to handle low-memory situations better.
Currently, when memory runs out (typically as a result of a background
thread loading a huge table from a database), an OutOfMemoryError gets
thrown. There's no telling *where* this error gets thrown; if you're
lucky, it happens in the table-loading thread so table loading stops
but the rest of the app keeps working; if you're unlucky, the AWT Event
or Repaint threads die, and then you're stuck with a fatally hosed
application.
Years ago, when programming on the Macintosh, I used an OS function
called SetGrowZone() to register a callback, which the memory manager
would invoke when it was having trouble fulfilling a memory allocation
request. My approach was to allocate a big chunk of memory at
application startup time, and when my GrowZone callback got called, I
would release that chunk of memory to give the application some extra
breathing room, and simultaneously display a dialog box warning the
user that memory was getting dangerously low.
I'm trying to find a way to implement such a warning mechanism in Java,
but all I have come up with so far are "soft references". The problem
is that the Java API document only says "Virtual machine
implementations are [...] encouraged to bias against clearing
recently-created or recently-used soft references."
It would be more helpful if a reference type existed that would
guarantee that its referent is only deleted as a last resort; with such
a reference I could simply create, say, a 5-megabyte object, point such
a reference at it, and in its finalizer, pop up a warning message...
But I don't see a reliable way to achieve this. Do I really have no
choice but to wait until an OutOfMemoryError hits me in the face? (I
guess I could periodically call Runtime.gc() followed by checking
Runtime.freeMemory() + Runtime.maxMemory() - Runtime.totalMemory()...
Not very elegant!)
- Thomas
can do to make the app use less memory, but I'm hoping that at least I
can find a way to handle low-memory situations better.
Currently, when memory runs out (typically as a result of a background
thread loading a huge table from a database), an OutOfMemoryError gets
thrown. There's no telling *where* this error gets thrown; if you're
lucky, it happens in the table-loading thread so table loading stops
but the rest of the app keeps working; if you're unlucky, the AWT Event
or Repaint threads die, and then you're stuck with a fatally hosed
application.
Years ago, when programming on the Macintosh, I used an OS function
called SetGrowZone() to register a callback, which the memory manager
would invoke when it was having trouble fulfilling a memory allocation
request. My approach was to allocate a big chunk of memory at
application startup time, and when my GrowZone callback got called, I
would release that chunk of memory to give the application some extra
breathing room, and simultaneously display a dialog box warning the
user that memory was getting dangerously low.
I'm trying to find a way to implement such a warning mechanism in Java,
but all I have come up with so far are "soft references". The problem
is that the Java API document only says "Virtual machine
implementations are [...] encouraged to bias against clearing
recently-created or recently-used soft references."
It would be more helpful if a reference type existed that would
guarantee that its referent is only deleted as a last resort; with such
a reference I could simply create, say, a 5-megabyte object, point such
a reference at it, and in its finalizer, pop up a warning message...
But I don't see a reliable way to achieve this. Do I really have no
choice but to wait until an OutOfMemoryError hits me in the face? (I
guess I could periodically call Runtime.gc() followed by checking
Runtime.freeMemory() + Runtime.maxMemory() - Runtime.totalMemory()...
Not very elegant!)
- Thomas