Smart resource system

I’m curious if anyone has had any success implementing a smart resource system in a Java game/engine.

What I’m referring to is a system in which resources are removed from memory (unloaded) once a specific threshold has been surpassed. A resource manager tracks each resource (via a LRU scheme or some such). When a new resource load is requested, the manager checks the threshold and unloads any previously loaded but currently uneeded resources if neccesary. When an active unloaded resource is later requested by the game, the manager loads it after first making room by unloading something else.

Considering that the only way to unload a resource in Java would be to null it and ensure that no references to it exist, I’m concerned about the potential for triggering the gc too often. Additionally, since the nulled reference will still be resident in memory, I’m also concerned about cases where a gc is not triggered - causing a resource to actually be in memory twice (the active, referenced one and the nulled one waiting to be gced).

This sort of system is very useful for games with large terrain sets, oodles of models, and numerous sound effects/music. By dividing the world into zones, only the resources belonging to the current and adjacent zones need be initialized at a given time. And of those, only a subset need actually be resident in memory.

Anyone have any experience with a Java implementation?

Isnt this something you would worry about with native bytebuffers? On the Java side the GC will look after all this for you whatever you try to do. If you allocate yourself a big direct bytebuffer, you could then look after it yourself, with the GC not being involved.

The fact that the GC looks after everything is what concerns me ;). Swapping lots of stuff in and out of memory is sure to cause more gc-induced pauses than I would like. Nothing is ever proven until implemented and tested, of course. Which is why I’m hoping someone around here has already been-there-done-that.

As for Bytebuffers, I don’t see how that would be different. There is still no way to implicitly free the memory. The direct memory would still be allocated until the ByteBuffer is itself gc’ed, correct?

Unless (after rereading) you are suggesting that I use a ByteBuffer to hold all of the resource data. That’s something I had not considered. But my first thoughts are that it would be way more trouble than it’s worth.

I’m concerned about the potential for triggering the gc too often

Actually, you want the GC to run often. If it has enough time to run often, there won’t be much to do - therefore no “halt of the world” effect.

Enabling vsync or using framecapping helps alot (usually you won’t have any gc pauses then). Also there are alot of neat switches for tweaking.

[quote]Considering that the only way to unload a resource in Java would be to null it and ensure that no references to it exist, I’m concerned about the potential for triggering the gc too often. Additionally, since the nulled reference will still be resident in memory, I’m also concerned about cases where a gc is not triggered - causing a resource to actually be in memory twice (the active, referenced one and the nulled one waiting to be gced).
[/quote]
Read up on Soft-References (there are several kinds, ‘Weak’, ‘Phantom’ etc…)
GC won’t do much if it doesn’t NEED to … if you aren’t allocating new resources, just having old stuff still in memory shouldn’t be a problem.
Soft References will solve your other problem of ‘resurrecting’ garbage.

http://java.sun.com/developer/technicalArticles/ALT/RefObj/

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/SoftReference.html