MemorySegment JVM memory leak

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Apr 23 00:34:32 UTC 2020


On 23/04/2020 00:22, Uwe Schindler wrote:
> With the proposed API it looks to me that one can do this:
> - Mmap a segment in a defined thread (I would use one that's created by the directory abstraction, so you always know which thread creates the segments) using MemorySegment#mapFromPath
> - Retrieve the MemoryAddress baseAddress() and its long size from the segment
> - Use the new API to create an unsafe segments from that address and size (call the above new method), without a cleaner and without a thread
>
> On close:
> - Close the original segment (needs to be done by the thread who created it, this is why I would spawn a thread to handle that)
> - Make sure to stop using the unsafe MemorySegment

I'm not sure I get the thread dance - the segment is unconfined, can't 
you just get rid of the confined segment and keep the unconfined one and 
then... just close it?

Btw, you can create the segment using mapFromPath, or you can also have 
your own piece of native code which does the mmap call with the 
parameters you want and wrap a segment around the address you get - your 
choice.

>
> Questions:
> - Is there a way to figure out if the user has set the command line option at runtime (without try-error)

For now that's a runtime property - so yes - getProperties :-)

We'll make sure that as the property will become something more robust 
(e.g. a real flag) we'll keep the runtime introspection in mind.

> - Can you mapFromPath using a custom offset? Of course you canmap the whole file and create a slice, but you are wasting address space if you don't need it. The code behind MappedMemorySegmentImpl uses FileChannel, but always passes 0L as startOffset. Can we add the offset too? Should not be to hard to add this missing parameter.
We could add an extra parameter - but also see above - 
ofNativeRestricted can be used to create a segment out of _any_ native 
address you might get (e.g. from a JNI call). You don't really need to 
be constrained by what's available in the API - although I agree that if 
something makes generally sense (and I think the offset parameter is in 
this category), then it should be added.
> - Would it be possible to create a memory mapped segment like described above and make it unsafe from the beginning?
No. But that doesn't seem to be a very important issue? If you don't 
need the original safe segment, you can just ditch it and leave it to 
the GC?
> - In which JDK 15 EA build can we test it, or do I need another build?

You can test it now on a Panama build (you will have to build it 
yourself) - or, hopefully you can try it out in a month or so in a Java 
15 EA build ;-)

In case you wanna try it now:

https://github.com/openjdk/panama-foreign/

Maurizio



More information about the panama-dev mailing list