<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi Martin,<br>
the OOM you are getting is not a Java heap OOM. It is caused by
the default limit set for off-heap memory. This is controlled by
the same option used for increasing memory available for direct
byte buffer (hence the exception message), namely:<br>
</p>
<div style="background-color:#ffffff;color:#000000;font-family:'Source
Code Pro',monospace;font-size:11.3pt;">
<pre><span style="color:#808080;font-style:italic;">-XX:MaxDirectMemorySize=<size>
</span></pre>
</div>
<p></p>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">For this reason, I doubt that what you
see on VisualVM correlates with the exception you are seeing (in
fact the seesaw pattern you attached seems to show that on-heap
memory usage is quite stable).</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">As to the actual causes, if your
application is using openConfined/openShared (w/o a cleaner
parameter!), then, assuming it still calls "MemorySession::close"
in the same places where it did in 17, nothing should have changed
(in fact, the underlying implementation between 17 and 19 did not
change much at all). In that case, all the memory segments
associated with the session have their off-heap memory deallocated
when the session is closed.<br>
</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">If you are using openImplicit, or
openConfined/Shared (with a cleaner parameter!) then you are at
the mercy of the GC - which means that you should make sure that
the implicit session containing no-longer needed data is truly
unreachable from within your application. <br>
</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">
<blockquote type="cite"><span> I can not unload() the
MemorySegment when I am done using it (but this was also not
possible in JDK17).</span></blockquote>
<br>
You can - but you have to plan ahead - e.g. if you need to
allocate a bunch of segments, then do something with them, and
then free them - then you need a temporary session where to
allocate those segments, and use a try-with-resource on the temp
session object.<br>
</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">Maurizio<br>
</div>
<br>
<div class="moz-cite-prefix">On 22/01/2023 17:09, Martin Pernollet
wrote:<br>
</div>
<blockquote type="cite" cite="mid:sIexasH-eocf_YyqHUHeLL2KaV7fDkyyX7vfjc5DxFqf7a66yvJi2GKFdrezqdXchLY3dXsd_ae3lYcbzFuySvKt_Kd1APozmduBRzYGDiM=@protonmail.com">
<div style="font-family: Arial; font-size: 14px;">Hi,</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">After switching
PanamaGL from JDK17 to JDK19, I am facing an OutOfMemory
exception.</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">The part where
this occurs is in the loop that performs offscreen rendering and
then copy the result to a BufferedImage later used for onscreen
rendering.</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">This allocation </div>
<div style="font-family: Arial; font-size: 14px;">
<div style="background-color:#2f2f2f;padding-left:2px">
<div style="color:#aaaaaa;background-color:#2f2f2f;font-family:"Menlo";font-size:12pt;white-space:nowrap">
<p><span style="color:#d9e8f7"> </span><span style="color:#80f2f6"></span></p>
<div style="background-color:#2f2f2f;padding-left:2px">
<div style="color:#aaaaaa;background-color:#2f2f2f;font-family:"Menlo";font-size:12pt">
<p><span style="color:#d9e8f7"> </span><span style="color:#cc6c1d">int</span><span style="color:#d9e8f7"> </span><span style="color:#f2f200">nBytes</span><span style="color:#d9e8f7"> </span><span style="color:#e6e6fa">=</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">width</span><span style="color:#d9e8f7"> </span><span style="color:#e6e6fa">*</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">height</span><span style="color:#d9e8f7"> </span><span style="color:#e6e6fa">*</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">channels</span><span style="color:#e6e6fa">;</span></p>
</div>
</div>
MemorySegment<span style="color:#d9e8f7"> </span><span style="color:#f2f200">pixelsRead</span><span style="color:#d9e8f7"> </span><span style="color:#e6e6fa">=</span><span style="color:#d9e8f7"> MemorySegment</span><span style="color:#e6e6fa">.</span><span style="color:#96ec3f;font-style:italic">allocateNative</span><span style="color:#f9faf4">(</span><span style="color:#f3ec79">nBytes</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7">
</span><span style="color:#66e1f8">scope</span><span style="color:#f9faf4">)</span><span style="color:#e6e6fa">;</span>
<p><span style="color:#d9e8f7"> </span><span style="color:#79abff">gl</span><span style="color:#e6e6fa">.</span><span style="color:#80f6a7">glReadPixels</span><span style="color:#f9faf4">(</span><span style="color:#6897bb">0</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7"> </span><span style="color:#6897bb">0</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">width</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">height</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">format</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7"> </span><span style="color:#66e1f8">textureType</span><span style="color:#e6e6fa">,</span><span style="color:#d9e8f7"> </span><span style="color:#f3ec79">pixelsRead</span><span style="color:#f9faf4">)</span><span style="color:#e6e6fa">;</span></p>
</div>
</div>
Fails as follow</div>
<div style="font-family: Arial; font-size: 14px;">
<div style="background-color:#2f2f2f;padding-left:2px">
<div style="color:#cccccc;background-color:#2f2f2f;font-family:"Menlo";font-size:12pt;white-space:nowrap">
<p><span style="color:#e11e46">java.lang.OutOfMemoryError:
Cannot reserve 2169600 bytes of direct buffer memory
(allocated: 4293080163, limit: 4294967296)</span></p>
<p><span style="color:#e11e46"> at
java.base/java.nio.Bits.reserveMemory(</span><span style="color:#6fc5ee;text-decoration:underline;text-decoration-style:solid">Bits.java:178</span><span style="color:#e11e46">)</span></p>
<p><span style="color:#e11e46"> at
java.base/java.nio.Buffer$1.reserveMemory(</span><span style="color:#6fc5ee;text-decoration:underline;text-decoration-style:solid">Buffer.java:860</span><span style="color:#e11e46">)</span></p>
<p><span style="color:#e11e46"> at
java.base/jdk.internal.foreign.NativeMemorySegmentImpl.makeNativeSegment(</span><span style="color:#6fc5ee;text-decoration:underline;text-decoration-style:solid">NativeMemorySegmentImpl.java:122</span><span style="color:#e11e46">)</span></p>
<p><span style="color:#e11e46"> at
java.base/java.lang.foreign.MemorySegment.allocateNative(</span><span style="color:#6fc5ee;text-decoration:underline;text-decoration-style:solid">MemorySegment.java:965</span><span style="color:#e11e46">)</span></p>
<p><span style="color:#e11e46"> at
java.base/java.lang.foreign.MemorySegment.allocateNative(</span><span style="color:#6fc5ee;text-decoration:underline;text-decoration-style:solid">MemorySegment.java:942</span><span style="color:#e11e46">)</span></p>
</div>
</div>
</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">As shown on the
VisualVM screenshot the heap grows at the beginning but is then
freed. The exception happens when the heap reduces suddenly on
the chart.</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;"><img alt="Capture d’écran 2023-01-22 à 17.48.40.png" class="proton-embedded" src="cid:part1.85VS5eYb.BlDjqtmY@oracle.com"> </div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">
<div><span>The behavior is the same for all possible scopes
provided by MemorySession (global, openConfined, openShared,
openImplicit.<br>
</span></div>
<div><span><br>
</span></div>
<div><span>One thing : I can not unload() the MemorySegment when
I am done using it (but this was also not possible in
JDK17).</span></div>
</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;"><b>How could I
track this memory leak? How could I ensure that useless memory
get destroyed?</b></div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">Thanks in
advance,</div>
<div style="font-family: Arial; font-size: 14px;"><br>
</div>
<div style="font-family: Arial; font-size: 14px;">Martin</div>
<div class="protonmail_signature_block" style="font-family: Arial;
font-size: 14px;">
<div class="protonmail_signature_block-user
protonmail_signature_block-empty"> </div>
<div class="protonmail_signature_block-proton"><br>
</div>
</div>
</blockquote>
</body>
</html>