如何垃圾收集直接缓冲java [英] how to garbage collect a direct buffer java
问题描述
我有一个内存泄漏,我已经隔离到错误处理的直接字节缓冲区。
I have a memory leak that I have isolated to incorrectly disposed direct byte buffers.
ByteBuffer buff = ByteBuffer.allocateDirect(7777777)
GC收集包含这些缓冲区的对象,但不会丢弃缓冲区本身。如果我实例化了足够的包含缓冲区的瞬态对象,我会得到这个令人鼓舞的消息。
The GC collects the objects that harbor these buffers but does not dispose of the buffer itself. If I instantiate enough of the transient objects containing buffers I get this encouraging message.
java.lang.OutOfMemoryError: Direct buffer memory
我一直在搜索这个问题,显然
I have been searching up this problem and apparently
buff.clear
和
System.gc()
不起作用
推荐答案
我怀疑你的应用程序的某个地方有对ByteBuffer实例的引用,这就是阻止它垃圾收集。
I suspect that somewhere your application has a reference to the ByteBuffer instance(s) and that is preventing it from being garbage collected.
直接ByteBuffer的缓冲区内存是在普通堆之外分配的(因此GC不会移动它!!)。但是,ByteBuffer API没有提供显式处理/解除分配缓冲区的方法。所以我假设垃圾收集器会这样做......一旦它确定不再引用ByteBuffer对象。
The buffer memory for a direct ByteBuffer is allocated outside of the normal heap (so that the GC doesn't move it!!). However, the ByteBuffer API provides no method for explicitly disposing of / deallocating a buffer. So I assume that the garbage collector will do it ... once it determines that the ByteBuffer object is no longer referenced.
这篇关于如何垃圾收集直接缓冲java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!