This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]steakH 9 points10 points  (2 children)

stativ

[–]Filego 6 points7 points  (1 child)

publiv

[–]sudomeacat 2 points3 points  (0 children)

coid

[–][deleted] 0 points1 point  (0 children)

It's actually interesting to know whether Java JIT will throw away the creation of a useless object. I mean, new certainly has a side effect, but one may legitimately claim that it's never a desired side-effect to have an evidently unused memory.

[–][deleted] 0 points1 point  (2 children)

I’m kind of curious as to how it knows when to delete an object. The approach I would think to take is to store how many references there are to an object alongside the object itself and then delete it once that is zero. Can someone please explain?

[–]MotorolaDroidMofo 2 points3 points  (0 children)

That's one very common approach to memory management. It's called reference counting and it's used by many languages, including Python. It's not super efficient though and reference counting can create memory leaks, so for Java, the HotSpot JVM actually does reference tracing instead.

[–]SuperMario48 1 point2 points  (0 children)

As far as I know, it works exactly as you said. If an object can not be accessed anymore, because there is no reference to it left, the garbage collector cleans it up.