Search code examples
javalambdaprofilingjava-8heap-dump

Java heap dump analysis for lambda expressions


My Java application heap dump shows that a specific lambda used in my class has locked up some amount of memory and its not being released during GC.

Heap shows the specific anonymous lambda class as ParentClass$$Lambda$ID and in current case, the ID is 79(image attached). This ID does not seem to have any relation with the number of lambdas that exist in the class and hence we cannot conclude on which lambda is represented. I'm interested to point at the exact lambda expression as it helps in analyzing, fixing and testing the scenarios related.

Decompiling the class file with DJ did not help as it recreates the lambda expressions to a readable code. Let me know if any ideas on this.

enter image description here


Solution

  • Try defining the system property

    jdk.internal.lambda.dumpProxyClasses=/path/to/dir
    

    when invoking the JVM. This will cause the runtime to write the dynamically generated lambda classes to disk, where you can inspect them with javap. This will enable you to see what fields (captured variables) they hold, and what lambda body method the lambda corresponds to.