Luke Taylor
Luke Taylor

Reputation: 9599

Java Garbage Collection object creation in iteration

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?

Upvotes: 1

Views: 550

Answers (7)

npgall
npgall

Reputation: 3028

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.

Upvotes: 1

David Titarenco
David Titarenco

Reputation: 33386

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.

Upvotes: 2

Cratylus
Cratylus

Reputation: 54074

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500155

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.

Upvotes: 3

Frank Pavageau
Frank Pavageau

Reputation: 11705

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

Upvotes: 1

duffymo
duffymo

Reputation: 308743

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.

Upvotes: 1

matt b
matt b

Reputation: 139921

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.

Upvotes: 1

Related Questions