UPD 21.11.2017: the bug is fixed in JDK, see comment from Vicente Romero
Summary:
If for
statement is used for any Iterable
implementation the collection will remain in the heap memory till the end of current scope (method, statement body) and won't be garbage collected even if you don't have any other references to the collection and the application needs to allocate a new memory.
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8175883
https://bugs.openjdk.java.net/browse/JDK-8175883
The example:
If i have the next code, which allocates a list of large strings with random content:
import java.util.ArrayList;
public class IteratorAndGc {
// number of strings and the size of every string
static final int N = 7500;
public static void main(String[] args) {
System.gc();
gcInMethod();
System.gc();
showMemoryUsage("GC after the method body");
ArrayList<String> strings2 = generateLargeStringsArray(N);
showMemoryUsage("Third allocation outside the method is always successful");
}
// main testable method
public static void gcInMethod() {
showMemoryUsage("Before first memory allocating");
ArrayList<String> strings = generateLargeStringsArray(N);
showMemoryUsage("After first memory allocation");
// this is only one difference - after the iterator created, memory won't be collected till end of this function
for (String string : strings);
showMemoryUsage("After iteration");
strings = null; // discard the reference to the array
// one says this doesn't guarantee garbage collection,
// Oracle says "the Java Virtual Machine has made a best effort to reclaim space from all discarded objects".
// but no matter - the program behavior remains the same with or without this line. You may skip it and test.
System.gc();
showMemoryUsage("After force GC in the method body");
try {
System.out.println("Try to allocate memory in the method body again:");
ArrayList<String> strings2 = generateLargeStringsArray(N);
showMemoryUsage("After secondary memory allocation");
} catch (OutOfMemoryError e) {
showMemoryUsage("!!!! Out of memory error !!!!");
System.out.println();
}
}
// function to allocate and return a reference to a lot of memory
private static ArrayList<String> generateLargeStringsArray(int N) {
ArrayList<String> strings = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
StringBuilder sb = new StringBuilder(N);
for (int j = 0; j < N; j++) {
sb.append((char)Math.round(Math.random() * 0xFFFF));
}
strings.add(sb.toString());
}
return strings;
}
// helper method to display current memory status
public static void showMemoryUsage(String action) {
long free = Runtime.getRuntime().freeMemory();
long total = Runtime.getRuntime().totalMemory();
long max = Runtime.getRuntime().maxMemory();
long used = total - free;
System.out.printf("%40s: %10dk of max %10dk%n", action, used / 1024, max / 1024);
}
}
compile and run it with limited memory, like this (180mb):
javac IteratorAndGc.java && java -Xms180m -Xmx180m IteratorAndGc
and at runtime i have:
Before first memory allocating: 1251k of max 176640k
After first memory allocation: 131426k of max 176640k
After iteration: 131426k of max 176640k
After force GC in the method body: 110682k of max 176640k (almost nothing collected)
Try to allocate memory in the method body again:
!!!! Out of memory error !!!!: 168948k of max 176640k
GC after the method body: 459k of max 176640k (the garbage is collected!)
Third allocation outside the method is always successful: 117740k of max 163840k
So, inside gcInMethod() i tried to allocate the list, iterate over it, discard the reference to the list, (optional)force garbage collection and allocate similar list again. But i can't allocate second array because of lack of memory.
In the same time, outside the function body i can successfully force garbage collection (optional) and allocate the same array size again!
To avoid this OutOfMemoryError inside the function body it's enough to remove/comment only this one line:
for (String string : strings);
<-- this is the evil!!!
and then output looks like this:
Before first memory allocating: 1251k of max 176640k
After first memory allocation: 131409k of max 176640k
After iteration: 131409k of max 176640k
After force GC in the method body: 497k of max 176640k (the garbage is collected!)
Try to allocate memory in the method body again:
After secondary memory allocation: 115541k of max 163840k
GC after the method body: 493k of max 163840k (the garbage is collected!)
Third allocation outside the method is always successful: 121300k of max 163840k
So, without for iterating the garbage successfully collected after discarding the reference to the strings, and allocated second time (inside the function body) and allocated third time (outside the method).
My supposition:
for syntax construction is compiled to
Iterator iter = strings.iterator();
while(iter.hasNext()){
iter.next()
}
(and i checked this decompiling javap -c IteratorAndGc.class
)
And looks like this iter reference stays in the scope till the end. You don't have access to the reference to nullify it, and GC can't perform the collection.
Maybe this is normal behavior (maybe even specified in javac, but i haven't found), but IMHO if compiler creates some instances it should care about discarding them from the scope after using.
That's how i expect to have the implementation of for
statement:
Iterator iter = strings.iterator();
while(iter.hasNext()){
iter.next()
}
iter = null; // <--- flush the water!
Used java compiler and runtime versions:
javac 1.8.0_111
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
Note:
the question is not about programming style, best practices,
conventions and so on, the question is about an efficiency of Java
platform.
the question is not about System.gc()
behavior (you may remove all
gc calls from the example) - during the second strings allocation the JVM must release the dicarded memory.
Reference to the test java class, Online compiler to test (but this resource has only 50 Mb of heap, so use N = 5000)
See Question&Answers more detail:
os