On 2/2/07, Leo Li <[EMAIL PROTECTED]> wrote:
Hi, Xiao-Feng: Excuse me if I am confusing you. The direct byte buffer holds a block of native memory space used in nio operation. The native byte buffer will be freed through the byte buffer is gc collected and added to ReferenceQueue monitored by MemorySpy series classes. But the gc will not be triggered if the java heap is still empty. Here is an example: I allocate a large block of native memory for a byte buffer while the byte buffer itself is quite small. Thus quickly native heap is depleted while VM still does not think gc is needed since java heap has a lot of free memory.import java.nio.*; public class Test { public static void main(String[] args) throws Exception { for(int i = 0;i<1000;i++) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(10240000); //System.gc(); } } } RI runs well but Harmony will soon throw OutOfMemory exception. But if remove the comment before System.gc(), Harmony will become ok. So my point is that we need a mechanism to notify VM to start gc if we have no more native memory because sometimes problem will be solved if gc is fired. Of cause we cannot avoid a user always malloc space but never free them.:)
This might be crazy, but is it possible or reasonable to have a method ByteBuffer.freeDirect()? It's a little big strange to me if API encourages this kind of behavior that programmer grabs resources freely while relying on certain unreliable behavior to release the resource. The situation is even worse than finalizer, well we know using finalizer is discouraged for resource management. Thanks, xiaofeng
