<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><p></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></p><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><p></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:9ccc6d7d@protonmail.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>