Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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 1376 times)
0 Members and 1 Guest are viewing this topic.
Offline gcsaba2

Junior Duke




Hello world


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

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

JGO Kernel


Medals: 409
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 Duke




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
Online princec

JGO Kernel


Medals: 409
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".  

YOU CAN NEVER FORCE COLLECTION OF AN OBJECT. YOU CAN NEVER BE GAURANTEED THAT ANY PARTICUALR OBJECT WILL GET COLLECTED.

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!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (18 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!