English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

If I'm managing hundreds of thousands of objects in a 3D world (for a game) should I store them all in a vector? But what happens if I have 100 coin objects, and one get's garbage collected, how would I know which one already had a variable created for it? And isn't there a runtime object thing that runs the garbage collector to free up memory? Does that mean what I have to do the coin1 = null thing? Since I'll be managing hundreds of thousands of objects, this could become a problem.

coin coin1 = new coin();
coin coin2 = new coin();
coin coin3 = new coin();
coin coin4 = new coin();

coin4 = null;
coin4 = new coin();

How would I know which coin was which until the compiler spits out an error? Or am I doing this totally wrong? If a player drops an item, it gets set to null. But does that free up memory or totally destory the object? And if the game automatically generates objects, how would it name them, especially if some of the them skip, such as coin, coin2, coin4, if coin3 is gone

2007-03-24 18:04:26 · 3 answers · asked by Invader Z 1 in Computers & Internet Programming & Design

Sorry this is is so long. I'll be grateful of any answer. I know I asked a lot of questions and some of them are kind of vague, I'll understand if you can't answer them or don't understand them. Thanks again.

2007-03-24 18:06:26 · update #1

3 answers

Typically you need to be instanciating these objects as part of some kind of collection class. You only have one instance (or as many instances as you need to distinguish collections), which can store N number of items (such as coin). When one is dropped, it could be removed from the collection and the GC would automatically pick it up.

Rather than referring to them by name, you would refer to them as something like CoinManager.Coins[index] or Player.CurrentlyHeldCoins() (returns an array of coin objects). If you needed to move a coin from player A to player B, you would not say Player1632454324.Coins.Add( coin809348348 ), you would say:

Players[currentPlayer].Coins.Add( Players[ currentPlayer.tradingPlayer ].RemoveCoin( currentAction.selectedCoin ) )

Though honestly, keeping a distinct instance of every object like a coin is a very inefficient way of doing it. It would probably be much faster to simply store a number on each player, of the number of coins that player has, or a number of coins of each denomiation (i.e. numberOfDimes, numberOfQuarters, etc.)

2007-03-24 18:11:33 · answer #1 · answered by Rex M 6 · 0 1

Essentially, in Java, you never worry about garbage collection. This is actually forced on you, because you can't control when garbage collection of an object will happen; it could happen as soon as an object goes out of scope, or not until the program terminates.

An object will only get garbage collected if its reference count drops to zero, meaning you don't have any variables referring to that spot in memory anymore. So an object won't get GC'ed until all the variables referencing it go out of scope, so you don't have to worry about "which one had a variable created for it."

Setting an object to null does not free the memory, as there could be other references to that object elsewhere. GC happens automatically when there is a need for memory. If you *really* need to, you can call System.gc(), but this can take a while.

As for managing objects, you could put them in a Vector, sure. Although you may want to use a linked list or something. If you're using threads, make sure you use one of the synchronized collections.

2007-03-25 01:16:03 · answer #2 · answered by leadingtoneseventh 2 · 1 0

Garbage collector is a low priority process/thread that Java runtime forces on you; I believe it is same that happens within .Net runtime although both supplies methods to "suggest" gc be run immediately, but coming back to low priority it would not be dictated upon to run immediately just because you say so, beginner books on Java or .Net make an important note on this behavior regarding GC. Whenever GC runs some object is deleted and the memory it once occupied is now marked free to use.

Anything generated to memory either in code or while program is running and do it by programming is referred to by a hash code sorta like you would (or not) refer a person by SIN number. The names that you make in source code is actually names assigned to pointers to the (stuff generated to memory).

So Coin c4 = new Coin(); is in fact making a pointer named c4 that points to a Coin object perhaps named Coin33429958.

If later Coin c5 = c4; c4 = null; is in fact making a pointer named c5 that points to a Coin object that c4 also point to, the one named Coin33429958; c4 is set to null but Coin33429958 is still being referred to by c5 so there is no GC happening and Coin33429958 still exists.

2007-03-25 01:52:49 · answer #3 · answered by Andy T 7 · 1 1

fedest.com, questions and answers