You create a total of 12 (possible) objects
list0 is a list containing from 1 to 10.- 10 individual elements ranging from 1 to 10. They can be interned, but that's another discussion.
- The return value is
list0.tail , which contains elements 2-10.
Here is a graph of conceptual memory immediately before the last brace, here are the links
Somewhere on JVM Stack list0(head)->1->2 ... ->10 | Top of JVM Stack, contains returnList with head->|
When the function is executed, the list0 link will be deleted and will have the right to garbage collection. Since list0 is the only thing referenced 1 , 1 is an honest game to collect garbage.
Elements 2-10, as well as the returned list containing them, will remain on the heap, since everyone who refers to example still accessible by executable code.
Hope this helps.
source share