1

I am currently making a game. I plan on creating a lot of instances of one class for the projectiles portion of my game. I was just wondering, since I will be creating many instances of this class, and the projectiles will have no use once they have rendered off the screen, is it really ok to just create them? I know there is a garbage collected in java, but how do I actually know that the instances of a class that are not used anymore will get deconstructed? Is this something I should really worry about?

3
  • No. You should not worry about destruction of the objects you create in Java. Only worry about closing them it they point to some stream or resource. Commented Oct 4, 2014 at 20:56
  • If the GC didn't collect objects that are not used anymore, wouldn't the GC be completely useless? Commented Oct 4, 2014 at 21:36
  • I suggest using Pool of objects, then you can recycle already created objects, and reduce garbage. Commented Oct 5, 2014 at 10:49

3 Answers 3

1

I know there is a garbage collected in java, but how do I actually know that the instances of a class that are not used anymore will get deconstructed?

Every object that has not a variable referencing it (a pointer) is eligible for garbage collection. This of course doesn't mean that it will be destroyed instantly as soon as it's no longer referenced but the Garbage Collector will take care of it and you shouldn't worry.

Sign up to request clarification or add additional context in comments.

5 Comments

The GC is much smarter than that. It doesn't count references. Otherwise, tree structure where a parent knows its children and the children know their parent (for example) would never be GCed.
@JB Nizet Where did I write how smart it is?. He is asking about "instances of the class that are not used anymore". I answered to the question.
Your answer says: "Every object that has not a variable referencing it (a pointer) is eligible for garbage collection.". That suggests that objects still having a reference to them are not eligible, and that you have to make sure all references are set to null to make objects eligible, which is wrong.
@JB Nizet He is worried about objects no longer referenced in the code (whether they would be garbage collected or not). I'm saying such an object like that is eligible for GC, don't worry. Anyway... I know that my english is not perfect, but still not sure if you're missinterpreting the answer. Saying that I know how to write doesn't mean that I'm not able to do other things...
I'm not saying that your answer is wrong. I'm saying that your answer could lead the OP, or future readers, to think that all references must be set to null for an object to be eligible to GC. Why don't you explain when an object is eligible to GC, in terms that are not subject to an incorrect interpretation? If I tell you "you can have a 10% price drop using the promo code FOO", my guess is that what you'll do is use the code FOO, and not BAR, even though saying that doesn't mean that BAR doesn't work to have a 10% price drop.
0

Java Virtual Machine (JVM) is responsible to invoke finalize method on all unused objects by calling:

 System.runFinalization();

or

 Runtime rt = Runtime.getRuntime();
       rt.runFinalization();

Once, JVM prepare the list of unused objects, it invokes the GC(Garbage Collector) by giving this list of unused objects.

And, if you want to invoke gc manually, use:

System.gc();
Runtime rt = Runtime.getRuntime();
rt.gc();

or use this directly:

Runtime.getRuntime.gc();

Important : But basically, you can't force JVM to invoke the GC. Rather, you can request and this request may or may not clean the memory.

Comments

0

Mostly it's fine to leave this kind of thing to the garbage collector. However, if you are worried about having loads of mostly-unused objects taking up bits of memory here and there, you could use an object pool. That's where you create a fixed number of objects, keep track of which you are using, and recycle them, so they are confined to one block of memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.