1

Take a look at the following code

for(int i = 0; i < 10; i++) {
    new Object();
}

Will the Garbage Collector kick in every iteration, since there is no reference to the object? Is it usually discouraged to create objects in iterations without any reference?

2
  • 2
    Modern GC are optimized for short lived objects, so this won't be so bad as you might think. It's better of course to avoid object creation but this is often premature optimization. Commented Sep 26, 2012 at 19:39
  • How did you check if your object is collected? Commented Sep 26, 2012 at 19:40

7 Answers 7

3

Will the Garbage Collector kick in every iteration, since there is no reference to the object?

Probably not. It's not like JVMs are generally reference counted as such. A small amount of garbage will be created - each object will be eligible for collection after the iteration, but when those objects are actually collected isn't specified.

Is it usually discouraged to create objects in iterations without any reference?

It's normally not a good idea to create objects for no purpose whatsoever. I'm not sure what you mean by "without any reference". If you've got a reason to create a fresh object in each iteration, and there's no need for it after the iteration has completed, that's fine.

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

Comments

2

Java makes no guarantees about when garbage collection kicks in. It only makes one guarantee regarding GC (and I paraphrase here):

Eventually, out-of-scope objects with a refcount of 0 will be garbage collected.

Even calling System.gc() will not guarantee anything. Stay away from any code that relies on non-deterministic behavior.

As Jon Skeet pointed out, most Java GCs don't use reference counting. Read more here http://www.ibm.com/developerworks/java/library/i-garbage2/ and here http://www.ibm.com/developerworks/java/library/i-garbage1/ (the articles are about IBMs VM - other VMs may vary in their GC algorithms). I was mainly using the terminology as an oversimplification.

3 Comments

There is usually no real refcount in Java, though
I wouldn't refer to refcount either, as that would suggest that two objects which refer to each other (and nothing else refers to them) will never be collected.
I thought whether or not VMs use refcounts is an implementation detail. I mainly used it as a simple way of saying "object has no more references". Sorry for any confusion >_<
1

No, the garbage collector does not kick in on every iteration. The collector is not exactly deterministic - there are not a specific set of constraints that cause it to run. The garbage collector does not run merely because you created objects that it can collect.

However, the Object created in each iteration is immediately eligible for garbage collection - these are two different concepts.

Comments

1

You don't know when the GC runs.

Every Object you created on the heap in the loop is eligible for GC when it goes out of scope, but you aren't guaranteed to run the GC every iteration.

Comments

1

The Garbage Collector kick in when the generation is full (or at a certain occupancy), not when a code block exits.

Comments

1

The comments about garbage collection being optimized for short-lived objects are valid.

Additionally, be aware that there are aspects of HotSpot which could also affect garbage collection in this case.

Escape Analysis was added to the Oracle/OpenJDK JVM in Java 6, and is enabled by default in the HotSpot compiler of OpenJDK 7:

Escape analysis is a technique by which the Java Hotspot Server Compiler can analyze the scope of a new object's uses and decide whether to allocate it on the Java heap.

In theory this would allow the compiler to determine that the object is only accessible within the method, and so it could be allocated on the stack instead of the heap. As such it could be freed from memory at the end of each iteration without requiring garbage collection at all (similar to freeing memory in C/C++ explicitly after each iteration).

In practice, the documentation for escape analysis in OpenJDK 7 says that the JDK 7 compiler currently "does not replace a heap allocation with a stack allocation for non-globally escaping objects". So it would appear that stack allocation is an optimization that could be implemented (IMO), but does not appear to be implemented right now.

Another optimization would be for the compiler (or HotSpot) to detect that the object created in this example is never used and that its constructor does not modify state/cause side effects elsewhere in the application. In that case it could eliminate the statement from the compiled code entirely and so new Object() would never be executed.

In summary, HotSpot continues to get smarter and has some optimizations for cases like this already.

Right now (as others have mentioned) GC is unlikely to kick in right after each iteration, but it will likely free these objects quickly nonetheless (eden space/young generation etc.).

In future HotSpot might use stack allocation and so indeed free the memory after each iteration. Or the compiler or HotSpot might eliminate this particular statement entirely.

Comments

0

Will the Garbage Collector kick in every iteration

You don't know and should not care as these object will be in the young generation, and the cleanup for that area is fast (requires small house keeping).

Is it usually discouraged to create objects in iterations without any reference?

Depends. If it like that i.e. creating anonymous objects in local scope could actually help GC to identify them as eligible for GC.

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.