Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to free images after they're not needed anymor  (Read 1578 times)
0 Members and 1 Guest are viewing this topic.
Offline gcsaba2

Junior Devvie

Hello world

« Posted 2005-04-25 11:26:44 »


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 Wink
Offline princec

« JGO Spiffy Duke »

Medals: 567
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #1 - Posted 2005-04-25 11:55:57 »

You will indeed need to do m = null if you want it to be cleared out by GC, as it will still be referenced, and therefore not collectable.

Cas Smiley

Offline gcsaba2

Junior Devvie

Hello world

« Reply #2 - Posted 2005-04-25 13:06:24 »

OK, so let's say I have the Menu and it contains some components in an array, like Comps cmp[];
Each component has a reference towards the Menu, such as "parent". When I write m = null; the reference to the menu will be lost, but those components still have the reference to it. It is, however, not accessible anymore, cause the components themselves are not referenced from the outside (they're private, for example). So, will the garbage collector free the memory in this case too, or will it be "careful" and not free the memory till the end of the game?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »

Medals: 567
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #3 - Posted 2005-04-25 13:16:19 »

It's just one simple rule: if it can't be reached by any Thread, it's garbage, no matter how many references there are to it.

Cas Smiley

Offline Jeff

JGO Coder

Got any cats?

« Reply #4 - Posted 2005-04-26 03:43:54 »

The GC is a true GC.  Things like circular references between dead objects will not keep them from being candidates for collection. As long as an object is not part of a graph that tracks back to a root reference (global, currently active stack ref, etc) it is a candidate for collection.

This is NOT to say however that it WILL get collected.  You used a bad word in your question, the word was "force".  


Collection is always at the discretion of the VM. Your only gauarntee is that it will make a "best effort" to collect all available memory before it gives up and throws an "out  of memory" exception.

In practice, this is rarely much of an issue.  Its only an issue if you are trying to count on things like finalizers or reference queues for time critical program logic.  Don't.  You have no gaurantee they will ever run.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (18 views)
2015-09-03 00:41:28

Andrew_3ds (29 views)
2015-09-01 19:08:10

afikri (20 views)
2015-08-31 09:30:22

afikri (27 views)
2015-08-31 09:30:07

afikri (14 views)
2015-08-31 09:27:24

afikri (17 views)
2015-08-31 09:26:40

Roquen (30 views)
2015-08-29 11:30:54

GamerC4 (38 views)
2015-08-22 20:38:50

GamerC4 (36 views)
2015-08-22 20:37:18

GamerC4 (42 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!