在java中删除整个BST

发布于 2024-12-27 02:53:05 字数 214 浏览 1 评论 0原文

我正在研究 Java 中 BST 的实现,当需要删除整个树时(例如使用 clear() 函数),它会递归地遍历整个树,将节点引用分配给 空。我想知道如果仅将树的根引用设置为 null,垃圾收集器是否能够收集为树分配的所有内存,因为在将根设置为 null 后,不会有任何对节点的实时引用无效的?如果这有效,是否需要运行多次垃圾收集器来收集整个树?

I am looking over implementations of BSTs in Java, and when the whole tree needs to be deleted (for example using the clear() function), it recursively goes through the whole tree assigning node references to null. I was wondering if the garbage collector would be able to collect all memory allocated for the tree if just the Root reference of the tree were set to null instead, since there wouldn't be any live references to the nodes after the Root is set to null? And if this works, would it take multiple garbage collector runs to collect the entire tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2025-01-03 02:53:05

我想知道如果只是将树的根引用设置为空而不将每个单个节点引用设置为空,那么java中的垃圾收集器是否能够收集为树分配的所有内存,因为不会有Root 设置为 null 后是否有对节点的活动引用?

是的。当节点网络的最后一个实时外部引用被删除时,所有节点都有资格进行垃圾收集。这听起来像你所描述的。

如果它能够收集树的内存,是否需要多次垃圾收集器运行才能遍历树的每个级别?

在简单的情况下,垃圾收集器可以一次性将它们全部删除。在任何使用标记(而不是引用计数)的垃圾收集器中,垃圾收集器不需要遍历垃圾对象网络。相反,它遍历并标记非垃圾对象,并将未标记为垃圾的对象视为垃圾。

实际上,复杂的垃圾收集器将堆划分为多个空间(例如,新一代和一个或多个旧代),并且可以(至少)独立地对其中的一些进行垃圾收集。如果您的树的节点已迁移到较旧的一代空间,那么可能需要多个 GC“周期”才能将它们全部删除。然而,你对此无能为力……而且你也不应该关心它。

I was wondering would the garbage collector in java be able to collect all memory allocated for the tree if just the Root reference of the tree were to be set to null without setting every single node reference to null since there wouldn't be any alive references to the nodes after the Root is set to null?

Yes. When the last live external reference to a network of nodes is removed, all of the nodes are eligible for garbage collection. This sounds like what you are describing.

And if it would be able to collect the memory of the tree would it take multiple garbage collector runs to go through each level of the tree?

In the simple case, the garbage collector can delete them all in one go. In any garbage collector that uses marking (rather than reference counts), the garbage collector does not need to traverse the network of objects that are garbage. Rather, it traverses and marks NON-garbage objects, and treats objects that it didn't mark as garbage.

In practice, sophisticated garbage collectors partition the heap into spaces (e.g. a new generation and one or more old generations) and can garbage collect (at least) some of them independently. If your tree's nodes have been migrated to an older generation space, then it could take more than one "cycle" of the GC to delete them all. However, there's not much you can do about this ... and you shouldn't care about it anyway.

笨死的猪 2025-01-03 02:53:05

一旦没有对树的任何节点的引用,垃圾收集器将能够回收整个树的内存。如果不存在对根节点的引用,但仍存在对子节点的可访问引用,则无法从内存中删除树。

想象一下垃圾收集器是如何工作的。它从各种根(静态、全局变量、作用域变量等)构建所有可访问内存的图表。任何仍可访问的内存都无法回收。如果单个节点仍然可访问,那么从该节点可访问的任何内容也无法回收(BST 节点的大多数实现都有父节点的访问器)。

The garbage collector will be able to reclaim the memory of the whole tree once there are no references to any of the nodes of the tree. If no references exist to the root node but an accessible reference still exists to a child node, then the tree can not be removed from memory.

Imagine how the garbage collector works. It builds a graph of all accessible memory from various roots (statics, globals, scoped variables, etc.). Any memory which is still accessible can not be reclaimed. If a single node is still accessible then anything that is accessible from it also can not be reclaimed (most implementations of nodes of BSTs have accessors for the parent node).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文