I have a method that create some heavy objects as cache. This objects are rarely accessed, but expensive and slow to process, and are initialized only on demand. Then on use my application, for instance, I could request about three heavy object like that, and reuse them, but too is possible that I run it once and it only occupy memory while I never use it anymore during a session.
My question is: is possible I define that an object is "garbage collectable", then in case of application requires more memory it unset this unused data?
I think that it should works something like that (pseudo-code):
private static MyInstance instance = null;
public static getInstance() {
if (instance == null) {
instance = calculate();
GC.put(instance);
}
return instance;
}
I think that I can do it with some kind of Timer, then check from time to time, but I guess that Java should have something like that, to call only if memory is heavy used.