I can't understand the difference between these two codes;
public static void main(String[] args) {
List<String> list = new ArrayList<>();
while (true) {
list.add("Hello");
}
}
which throws java.lang.OutOfMemoryError
within a second,
AND
public static void main(String[] args) {
List<String> list = new ArrayList<>();
while (true) {
list.add("Hello");
System.out.println(list.size()); // Simply display the size of List
}
}
which throws java.lang.OutOfMemoryError
after 5 minutes with the list.size() having the value 20767725.
Simply put - it takes quite a while to display 20 million lines of text.
It's easy enough to show that. Run this code:
for (int x = 0; x < 20767725; x++) {
System.out.println(x);
}
I suspect that'll take about 5 minutes as well.
See more on this question at Stackoverflow