本帖最后由 xhz 于 12-20 15:08 编辑
clear()
为了让GC更快可以回收放置的元素,需要将node之间的引用关系赋空。
- /**
- * Removes all of the elements from this list.
- * The list will be empty after this call returns.
- */
- public void clear() {
- // Clearing all of the links between nodes is "unnecessary", but:
- // - helps a generational GC if the discarded nodes inhabit
- // more than one generation
- // - is sure to free memory even if there is a reachable Iterator
- for (Node<E> x = first; x != null; ) {
- Node<E> next = x.next;
- x.item = null;
- x.next = null;
- x.prev = null;
- x = next;
- }
- first = last = null;
- size = 0;
- modCount++;
- }
复制代码
------
原文链接:https://pdai.tech/md/java/collection/java-collection-LinkedList.html
|