<div dir="ltr">Hi Zhengyu,<div>What is your usage pattern of these single-abstract-method implementations? Since it sounds like you are creating a lot of them, are you storing them in collections?</div><div>If you are keeping a lot of them in collection (say, as event handlers), you may try to use `MethodHandleProxies.asInterfaceInstance` as a temporary workaround on JDK 22 and higher (older version uses Proxy, which has horrible invocation performance).</div><div>If you are on older versions from 15 to 21, unfortunately you might have to write a hidden class for the same purpose or use an existing library. One library that might be useful is <a href="https://github.com/LanternPowered/Lmbda">https://github.com/LanternPowered/Lmbda</a> that effectively generates unloadable hidden classes, but its 3.x builds are not maven central so you have to build yourself.</div><div><br></div><div>- Chen</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, May 29, 2024 at 3:35 PM Zhengyu Gu <<a href="mailto:zhengyu.gu@servicenow.com">zhengyu.gu@servicenow.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 class="msg-2455228651006166615">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_-2455228651006166615WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">Hi Chen,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Thanks for the insights.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">We did refactor our code to avoid using LambdaMetaFactory,metafactory() directly.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">With increasing use of Lambdas, in our applications and libraries, the metaspace impact becomes a concern. If current implementation (not able to unload unused Lambda classes) here to stay, we must come up
with a coding guideline to avoid excessive creation of Lambda classes, any pointers or suggestions would be greatly appreciated.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Best,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">-Zhengyu<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div id="m_-2455228651006166615mail-editor-reference-message-container">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="color:black">From:
</span></b><span style="color:black">Chen Liang <<a href="mailto:liangchenblue@gmail.com" target="_blank">liangchenblue@gmail.com</a>><br>
<b>Date: </b>Wednesday, May 29, 2024 at 2:43</span><span style="font-family:Arial,sans-serif;color:black"> </span><span style="color:black">PM<br>
<b>To: </b>Zhengyu Gu <<a href="mailto:zhengyu.gu@servicenow.com" target="_blank">zhengyu.gu@servicenow.com</a>><br>
<b>Cc: </b><a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a> <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Subject: </b>Re: Question on Lambda function<u></u><u></u></span></p>
</div>
<div>
<div>
<h3><strong><span style="font-family:Helvetica;color:black;background:yellow">[External Email]</span></strong><span style="font-family:Helvetica"><u></u><u></u></span></h3>
<p class="MsoNormal"><span style="font-family:Helvetica"><u></u> <u></u></span></p>
<div class="MsoNormal" align="center" style="text-align:center"><span style="font-family:Helvetica">
<hr size="0" width="100%" align="center">
</span></div>
</div>
<p class="MsoNormal">Hi Gu,</p>
<div>
<p class="MsoNormal">CallSite is specific to each invokedynamic instruction instead of each InvokeDynamic constant pool entry:
<a href="https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-6.html#jvms-6.5.invokedynamic" target="_blank">
https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-6.html#jvms-6.5.invokedynamic</a></p>
</div>
<div>
<p class="MsoNormal">And the linking is done by MethodHandleNatives.linkCallSite if you want to follow the Java implementation code.</p>
</div>
<div>
<p class="MsoNormal">For why the lambda in the loop is constant, it's a feature from InnerClassLambdaMetafactory: <a href="https://github.com/openjdk/jdk/blob/c8eea59f508158075382079316cf0990116ff98e/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java#L236" target="_blank">https://github.com/openjdk/jdk/blob/c8eea59f508158075382079316cf0990116ff98e/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java#L236</a></p>
</div>
<div>
<p class="MsoNormal">When the lambda is non-capturing, the bootstrap method LambdaMetafactory.metafactory will eagerly create a singleton instance and return this singleton in the indy instruction.</p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Also, your metaspace pressure might be caused by the fact that Lambda classes (not instances) are no longer eagerly unloaded; see <a href="https://github.com/openjdk/jdk/pull/12493" target="_blank">https://github.com/openjdk/jdk/pull/12493</a> and <a href="https://bugs.openjdk.org/browse/JDK-8302154" target="_blank">https://bugs.openjdk.org/browse/JDK-8302154</a>.
You are recommended to create your own facility to create hidden classes in Java 17 instead of continue to use LambdaMetafactory explicitly in code.</p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Regards,</p>
</div>
<div>
<p class="MsoNormal">Chen Liang</p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Wed, May 29, 2024 at 12:53<span style="font-family:Arial,sans-serif"> </span>PM Zhengyu Gu <<a href="mailto:zhengyu.gu@servicenow.com" target="_blank">zhengyu.gu@servicenow.com</a>> wrote:</p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<div>
<div id="m_-2455228651006166615m_-5447452561659455976mail-editor-reference-message-container">
<div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34);background:white">Hello Lambda experts,</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">Since we upgraded JDK from 11 to 17, we’re experiencing metaspace pressure, largely due to Lambda class implementation
changes.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">There’s a scenario (see attached test case), that is especially puzzled me, hopefully, you can share some insights.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">In this test case, there is only one Lambda class is created inside the loop, but each one for the same functions outside
loop.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">Example output:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">0: Func = LambdaFunc$$Lambda/0x00001f80000c4a20@4de8b406</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">testMethod() called</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">1: Func = LambdaFunc$$Lambda/0x00001f80000c4a20@4de8b406</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">testMethod() called</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">2: Func = LambdaFunc$$Lambda/0x00001f80000c4a20@4de8b406</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">testMethod() called</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">3: Func = LambdaFunc$$Lambda/0x00001f80000c4a20@4de8b406</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">testMethod() called</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">4: Func = LambdaFunc$$Lambda/0x00001f80000c4a20@4de8b406</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)">testMethod() called</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(34,34,34)"> </span><u></u><u></u></p>
<p class="MsoNormal">….<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Outside loop1, Func = LambdaFunc$$Lambda/0x00001f80000c4c58@402f32ff<u></u><u></u></p>
<p class="MsoNormal">testMethod() called<u></u><u></u></p>
<p class="MsoNormal">Outside loop2 Func = LambdaFunc$$Lambda/0x00001f80000d1000@5ae9a829<u></u><u></u></p>
<p class="MsoNormal">testMethod() called<u></u><u></u></p>
<p class="MsoNormal">Outside loop3 Func = LambdaFunc$$Lambda/0x00001f80000d1238@548b7f67<u></u><u></u></p>
<p class="MsoNormal">testMethod() called<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">And jcmd also confirmed there were 4 Lambda classes created:<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> 49: CLD 0x000060000134cb50: "app" instance of jdk.internal.loader.ClassLoaders$AppClassLoader<u></u><u></u></p>
<p class="MsoNormal"> Loaded classes:<u></u><u></u></p>
<p class="MsoNormal"> 1: LambdaFunc$$Lambda/0x00001f80000d1238<u></u><u></u></p>
<p class="MsoNormal"> 2: LambdaFunc$$Lambda/0x00001f80000d1000
<u></u><u></u></p>
<p class="MsoNormal"> 3: LambdaFunc$$Lambda/0x00001f80000c4c58<u></u><u></u></p>
<p class="MsoNormal"> 4: LambdaFunc$$Lambda/0x00001f80000c4a20<u></u><u></u></p>
<p class="MsoNormal"> 5: LambdaFunc<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Looking into bytecode, all four call sites have the same invokedynamic bytecode (invokedynamic #7, 0 // InvokeDynamic #0:run:()Ljava/lang/Runnable; ) and the first
invokedynamic bytecode is inside the loop.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">But when I ran the program with -XX:+TraceBytecodes, it seems that the first invokedynamic was hoisted and result was used in the subsequence loop.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Can anyone explain where this magic happens? If the magic can apply to the instances outside the loop, so that only one Lambda class is created?<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thank you for your time and expertise,<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">-Zhengyu<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div></blockquote></div>