<div dir="ltr"><div dir="ltr">I am kind of annoyed this doesn't solve the problem (scoping the allocation):<div><br></div><div><pre style="color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">package </span><span style="color:rgb(0,0,0)">org.example</span>;<br><br><span style="color:rgb(0,51,179)">import </span><span style="color:rgb(0,0,0)">java.util.concurrent.ExecutionException</span>;<br><span style="color:rgb(0,51,179)">import </span><span style="color:rgb(0,0,0)">java.util.concurrent.Executors</span>;<br><br><span style="color:rgb(0,51,179)">public class </span><span style="color:rgb(0,0,0)">Main1 </span>{<br> <span style="color:rgb(0,51,179)">public static void </span><span style="color:rgb(0,98,122)">main</span>(<span style="color:rgb(0,0,0)">String</span>[] args) <span style="color:rgb(0,51,179)">throws </span><span style="color:rgb(0,0,0)">ExecutionException</span>, <span style="color:rgb(0,0,0)">InterruptedException </span>{<br> <span style="color:rgb(0,51,179)">try </span>(<span style="color:rgb(0,51,179)">var </span><span style="color:rgb(0,0,0)">executor </span>= <span style="color:rgb(0,0,0)">Executors</span>.<span style="font-style:italic">newVirtualThreadPerTaskExecutor</span>()) {<br> <span style="color:rgb(0,51,179)">var </span><span style="color:rgb(0,0,0)">future </span>= <span style="color:rgb(0,0,0)">executor</span>.submit(() -> {<br> <span style="color:rgb(0,51,179)">try </span>{<br> <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"Starting work"</span>);<br> {<br> <span style="color:rgb(0,51,179)">var </span><span style="color:rgb(0,0,0)">bigBuffer </span>= <span style="color:rgb(0,51,179)">new byte</span>[<span style="color:rgb(23,80,235)">1024 </span>* <span style="color:rgb(23,80,235)">1024 </span>* <span style="color:rgb(23,80,235)">1024</span>];<br> <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"bigBuffer size: " </span>+ <span style="color:rgb(0,0,0)">bigBuffer</span>.<span style="color:rgb(135,16,148)">length</span>);<br> }<br> <span style="color:rgb(140,140,140);font-style:italic">// bigBuffer = null;<br></span><span style="color:rgb(140,140,140);font-style:italic"> </span><span style="font-style:italic">slowIO</span>();<br> } <span style="color:rgb(0,51,179)">catch </span>(<span style="color:rgb(0,0,0)">InterruptedException </span>e) {<br> <span style="color:rgb(0,51,179)">throw new </span>RuntimeException(e);<br> }<br> });<br> <span style="color:rgb(0,0,0)">future</span>.get();<br> }<br> }<br><br> <span style="color:rgb(0,51,179)">private static void </span><span style="color:rgb(0,98,122)">slowIO</span>() <span style="color:rgb(0,51,179)">throws </span><span style="color:rgb(0,0,0)">InterruptedException </span>{<br> <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"Starting slowIO"</span>);<br> <span style="color:rgb(0,0,0)">Thread</span>.<span style="font-style:italic">sleep</span>(<span style="color:rgb(0,0,0)">Long</span>.<span style="color:rgb(135,16,148);font-style:italic">MAX_VALUE</span>);<br> }<br>}</pre></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Nov 6, 2022 at 11:16 PM Sam Pullara <<a href="mailto:spullara@gmail.com">spullara@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I would be surprised if that memory is ever released, JITed or not unless hotspot starts to try and determine object lifetimes per line. I'd be curious and will run it myself what happens if you just put a block around the allocation and println.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Nov 5, 2022 at 10:28 AM Arnaud Masson <<a href="mailto:arnaud.masson@fr.ibm.com" target="_blank">arnaud.masson@fr.ibm.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<div lang="FR">
<div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:34.8pt"><span lang="EN-US">In the sync case, the big buffer is retained in heap during the slow IO (until it eventually returns), even if it’s not needed anymore.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:34.8pt"><span><u></u> <u></u></span></p>
</blockquote>
<p class="MsoNormal" style="margin-left:34.8pt"><span>Are you sure you see an issue there once this code is warmed up? If so, are you running this in the IDE with the debugger (asking because the debugger need to access locals
will prevent the compiler from pruning locals).<br>
<br>
<br>
<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I just execute the main() entry once _<i>without</i>_ debugger (from IDE).<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I then check memory usage in VisualVM, and I force GC a few times to be sure memory is really retained.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">The async app releases the big buffer but not the sync app. (The sync app releases memory if I uncomment the null assignment.)<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">By warmed-up, you mean it has been JIT-ed? It’s a bit outside the control of a normal app, is it really supposed to have such an impact on object strong ref lifetime?
<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">thanks<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Arnaud<u></u><u></u></span></p>
</div>
<div>
Unless otherwise stated above:<br>
<br>
Compagnie IBM France<br>
Siège Social : 17, avenue de l'Europe, 92275 Bois-Colombes Cedex<br>
RCS Nanterre 552 118 465<br>
Forme Sociale : S.A.S.<br>
Capital Social : 664 069 390,60 €<br>
SIRET : 552 118 465 03644 - Code NAF 6203Z<br>
</div></div>
</div></blockquote></div>
</blockquote></div>