Hi,
Let’s say I have a game design like this:
Game is the main class. It contains many objects, like Map, list of Units, Menu etc.
The Menu object contains the stuff needed for the startup menu, not the game itself. It contains some flashy images that take pretty much memory, and they’re stored in a BufferedImage.
OK, so when the Game contains a public field like Menu m;
When the game starts I write m = new Menu();
while ( !m.startGame() )
{
// do stuff
}
When startGame() becomes true, the Menu should be deallocated from the memory, cause it won’t be used anymore, at least not until the player clicks on the “Back to Main menu” button, in which case it will load the images again.
What I’d like to know is how to force Java to unload those images? Let’s say each image takes a couple of megabytes, when the actual game starts this will be too much so the Menu object has to be unloaded.
I was thinking of writing
m = null;
and then the garbage collector will pick it up. Will this work? After that line, if I write System.gc(); will the memory be freed immediately?
Waiting for your reply