c# how to delete an instance after you throw it around the program?

Started by
13 comments, last by jpetrie 10 years ago

ok thnx guys i got it

one last question, what is the "right" and more elegant way to organize it in a OOP? a container class like world ? or just a totally different way?

i think my problem is pretty common, every game has to create and delete objects very often in every game...

Advertisement

ok thnx guys i got it

one last question, what is the "right" and more elegant way to organize it in a OOP? a container class like world ? or just a totally different way?
i think my problem is pretty common, every game has to create and delete objects very often in every game...

Depends a lot on how complex your game is and how objects are arranged in the game. For instance a game like minecraft when you break a block it basically goes: "Hey world object, remove this tree block I just broke at these map coordinates." then it sets the block to nothing in the map data.

For a game with something like AI enemies, maybe soldiers, you could say have them running around a "map" or "world" object in a container, maybe they were called "entities". In that case maybe the soldiers would die after awhile and spawn a ragdoll or be set as one, then they would keep a timer counting how long they've existed as ragdolls. Then you could have the world/map remove tell them to "shimmer out" after a certain period of time, and set themselves to dead for permanent removal after the effect has finished.

As you can see when and how you remove the objects sort of depends on what kind of objects exist in your game and how things interact with them. There's not really a "golden rule to fit all" situation.

If you no longer have any references to the data that you want to clear then you can force a garbage collection.

data = null;

GC.Collect();

If you no longer have any references to the data that you want to clear then you can force a garbage collection.

data = null;

GC.Collect();

Don't do that.

The GC.Collect() garbage collection command runs a "stop the world" algorithm, running the full garbage collection routine on every generation of the data. It will stop your entire game and pause until it is done running.

Depending on your version of C#, it either uses a background collector or a concurrent collector. Both of them are really good at not interfering with your app and generally only run when your program is idle or blocked.

If you no longer have any references to the data that you want to clear then you can force a garbage collection.

data = null;

GC.Collect();

Don't do that.

The GC.Collect() garbage collection command runs a "stop the world" algorithm, running the full garbage collection routine on every generation of the data. It will stop your entire game and pause until it is done running.

Depending on your version of C#, it either uses a background collector or a concurrent collector. Both of them are really good at not interfering with your app and generally only run when your program is idle or blocked.

This. You also screw with the internal heuristics used by the CLR. It's generally quite a bad idea to force a garbage collection.

This topic is closed to new replies.

Advertisement