gpt4 book ai didi

java - 我们是否必须手动释放分配的 ByteBuffer?

转载 作者:行者123 更新时间:2023-12-01 14:17:31 29 4
gpt4 key购买 nike

我正在写一些使用 ByteBuffer 的东西s。在 docs of the API它说

There is no way to free a buffer explicitly (without JVM specific reflection). Buffer objects are subject to GC and it usually takes two GC cycles to free the off-heap memory after the buffer object becomes unreachable.



然而在 SO post's accepted answer我读

BigMemory uses the memory address space of the JVM process, via direct ByteBuffers that are not subject to GC unlike other native Java objects.



现在我该怎么办,我应该释放创建的缓冲区吗?还是我误解了文档或答案中的某些内容?

最佳答案

这取决于您如何创建缓冲区,有许多可能的用例。普通 ByteBuffer.allocate()将在堆上创建并由 GC 收集。其他选项,例如 native 内存可能不会。

Terracotta BigMemory 是一种不受 JVM GC 控制的原生堆外内存。如果在这种类型的内存中分配缓冲区,则必须自己清除它。

即使缓冲区已分配在堆内存中,清除缓冲区也可能是一个好主意。 GC 将负责收集未使用的缓冲区,但这需要一些时间。

关于java - 我们是否必须手动释放分配的 ByteBuffer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55417923/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com