Reputation: 1031
If I have a List[HugeObjects]
with numerous elements that I have to iterate only once and no operations are made on it (e.g. appending, prepending, etc), does it make sense that I convert that List
into an Iterator
, so when I iterate it the elements will get removed and more memory will be available (because GC will take care of them at some point)?
Upvotes: 3
Views: 1750
Reputation: 170745
I expected the answer to be "No, the iterator keeps a reference to the list, so as long as you use the iterator nothing will be garbage-collected."
But after looking at the actual implementation (https://github.com/scala/scala/blob/v2.12.2/src/library/scala/collection/LinearSeqLike.scala#L41), it turns out it doesn't: there is a comment which specifically mentions not "prevent[ing] original seq from garbage collection", and
List("a").iterator.getClass().getDeclaredFields()
confirms these
is the only field and so there is no hidden reference.
But! You don't say what is the operation you want to do on the List
, but if you can convert to iterator, then the List
code also probably loses references to the beginning of the list, and the garbage collector can collect it: When is a Java local variable eligible for GC?. There might be some differences in code generated by Scala which would block this optimization, but I wouldn't expect so.
Upvotes: 3