3

Are there features in Java to tell the runtime about unmanaged memory allocation like the GC.AddMemoryPressure method in .NET?

Thomas Owens
117k100 gold badges322 silver badges439 bronze badges
asked Sep 6, 2011 at 12:41
3
  • 2
    What's this stuff about "quality standards"? Commented Sep 6, 2011 at 12:45
  • That is a place holder text to pass stackoverflow quality standards checker. Commented Sep 6, 2011 at 12:48
  • @misha nesterernko Place holder text? If it's not part of the question then it has no need to be there. Commented Sep 6, 2011 at 12:49

2 Answers 2

2

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:

answered Sep 6, 2011 at 12:46
Sign up to request clarification or add additional context in comments.

3 Comments

Strictly, the two runtime systems make different assumptions about memory management, with Java/JRE simply assuming that this isn't very important.
Managed object can hold big amount of native memory and rather small amount of managed memory, so for gc it looks ok and it does not collect that objects. After some time native heap is full and OutOfMemoryException is thrown. That is like problem with Deflater class. I have found this issue at: devguli.com/blog/eng/java-deflater-and-outofmemoryerror. That is suggested there to call end() method manually. I think it is some kind of a tradeof that has to be avoided using garbage collector (we do not have to bother about memory deallocation when gc is used)
@Donal Fellows: Can you point to some readings? I do not want to challange that statement, just interested!
2

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)

answered Sep 6, 2011 at 13:41

2 Comments

this is not directly related to GC.AddMemoryPressure. This would imply re-writing your application to perform all native memory allocation through java. For many native libraries this would be unacceptable.
@yano Hopefully you write most of your code in Java (or another high level language like C#) and only write a minimum in native code, if at all.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.