LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 153|回复: 0

LinkedList实现 - clear()

[复制链接]
发表于 2023-12-20 15:07:14 | 显示全部楼层 |阅读模式
本帖最后由 xhz 于 12-20 15:08 编辑

clear()


为了让GC更快可以回收放置的元素,需要将node之间的引用关系赋空。

  1.     /**
  2.      * Removes all of the elements from this list.
  3.      * The list will be empty after this call returns.
  4.      */
  5.     public void clear() {
  6.         // Clearing all of the links between nodes is "unnecessary", but:
  7.         // - helps a generational GC if the discarded nodes inhabit
  8.         //   more than one generation
  9.         // - is sure to free memory even if there is a reachable Iterator
  10.         for (Node<E> x = first; x != null; ) {
  11.             Node<E> next = x.next;
  12.             x.item = null;
  13.             x.next = null;
  14.             x.prev = null;
  15.             x = next;
  16.         }
  17.         first = last = null;
  18.         size = 0;
  19.         modCount++;
  20.     }
复制代码


------
原文链接:https://pdai.tech/md/java/collection/java-collection-LinkedList.html


您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表