Are there features in Java to tell 开发者_如何学JAVAthe runtime about unmanaged memory allocation like the GC.AddMemoryPressure method in .NET?
I guess this is not required in Java. 'If a small managed object allocates a large amount of unmanaged memory' can simply not happen in Java, if you call native (unmanaged) functions via JNI the memory is not assigned to the JVM's managed memory table.
For reference:
- Java memory Management for JNI
The direct memory is managed separately in Java and could be considered "unmanaged". In the Sun/Oracle JVM it has its own memory usage limit and you can reach the limit of the heap size and the direct memory size at once.
In the Sun/Oracle JVM you can explicitly free a direct memory block using an internal API. It is rare you should even need to do so but here is how
ByteBuffer bb = ByteBuffer.allocateDirect(1024*1024);
((DirectBuffer) bb).cleaner().clean();
You can do this a ten thousand times without triggering a GC.
BTW: Memory mapped files use a small amount of heap and doesn't count to the direct memory limit. The amount you can map in is practically unlimited. (Though limited to 2GB in one MappedByteBuffer)
精彩评论