GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Classloader memory leak via JNI global references · Issue #13480 · netty/netty

Classloader memory leak via JNI global references #13480

New issue
Copy link
New issue
Copy link
Closed
Closed
Classloader memory leak via JNI global references#13480
#13584 (+2)
Copy link
Assignees
normanmaurer
Milestone
4.1.98.Final

Description

@SgtSilvio
SgtSilvio
opened on Jul 10, 2023
Issue body actions

Expected behavior

Netty (in this case via reactor-netty) can be used in conjunction with multiple classloaders, allowing classloaders to be properly unloaded if not referenced anymore. (in this case this is important for a gradle plugin)

Actual behavior

When using netty-resolver-dns-native-macos, a classloader and all its classes can not be unloaded even if not referenced anymore as the class io.netty.resolver.dns.macos.DnsResolver is a GC root as a JNI global reference.

This class (and other required classes) are loaded via NETTY_JNI_UTIL_LOAD_CLASS (

netty/resolver-dns-native-macos/src/main/c/netty_resolver_dns_macos.c

Line 186 in 79d3fa5

NETTY_JNI_UTIL_LOAD_CLASS(env, dnsResolverClass, nettyClassName, done);
) which uses NewGlobalRef (https://github.com/netty/netty-jni-util/blob/8d820fba781882f8a7fe94efb7656d1413ff8bb8/src/main/c/netty_jni_util.h#L45).

Maybe using weak global references instead of global references of the classes is a solution.

Or is there any other solutions how to safely use netty with dynamic classloaders?

Netty version

4.1.94.Final via reactor-netty:1.1.8

JVM version (e.g. java -version)

Any

OS version (e.g. uname -a)

MacOS

Activity

Next
  1. normanmaurer commented on Jul 10, 2023

    @normanmaurer
    normanmaurer
    on Jul 10, 2023
    Member
    More actions

    I think the problem is that we don't call NETTY_JNI_UTIL_UNLOAD_CLASS for it ... Let me fix this

  2. normanmaurer
    added a commit that references this issue on Jul 10, 2023

    Fix Classloader memory leak via JNI global references

    a5a962c
  3. normanmaurer commented on Jul 10, 2023

    @normanmaurer
    normanmaurer
    on Jul 10, 2023
    Member
    More actions

    @SgtSilvio see #13481

  4. normanmaurer
    added this to the 4.1.95.Final milestone on Jul 10, 2023
  5. SgtSilvio commented on Jul 10, 2023

    @SgtSilvio
    SgtSilvio
    on Jul 10, 2023
    ContributorAuthor
    More actions

    Thanks for the quick answer and nice catch. It fixes definitely an issue, but are you sure it is enough to allow the GC to collect the classloader? I thought the unload of the native library will only happen once the classloader is ready for GC, but I might be mistaken (as my JNI knowledge is very limited).

  6. normanmaurer commented on Jul 11, 2023

    @normanmaurer
    normanmaurer
    on Jul 11, 2023
    Member
    More actions

    @SgtSilvio I think it should work, that said I might also be wrong :)

  7. SgtSilvio commented on Jul 11, 2023

    @SgtSilvio
    SgtSilvio
    on Jul 11, 2023
    ContributorAuthor
    More actions

    ok, I want to try this out. Is there an easy way to get a snapshot of netty-resolver-dns-native-macos. Or which steps do I need to take to build it myself? A simple maven package does not seem to be enough.

  8. normanmaurer
    closed this as completedin #13481on Jul 11, 2023
  9. normanmaurer
    added a commit that references this issue on Jul 11, 2023

    Fix Classloader memory leak via JNI global references (#13481)

    09c8f60
  10. normanmaurer commented on Jul 11, 2023

    @normanmaurer
    normanmaurer
    on Jul 11, 2023
    Member
    More actions

    @SgtSilvio this should work:

    ./mvnw -pl transport-native-unix-common,resolver-dns-native-macos,resolver-dns-classes-macos clean package
    
  11. SgtSilvio commented on Jul 11, 2023

    @SgtSilvio
    SgtSilvio
    on Jul 11, 2023
    ContributorAuthor
    More actions

    getting the following error:

    [ERROR] Failed to execute goal org.fusesource.hawtjni:hawtjni-maven-plugin:1.18:build (build-native-lib) on project netty-resolver-dns-native-macos: build failed: org.apache.maven.plugin.MojoExecutionException: Requested download does not exist.: The following artifacts could not be resolved: io.netty:netty-resolver-dns-native-macos:zip:native-src:4.1.95.Final-SNAPSHOT (absent)

    Fixed it by following https://netty.io/wiki/setting-up-development-environment.html

  12. SgtSilvio commented on Jul 12, 2023

    @SgtSilvio
    SgtSilvio
    on Jul 12, 2023
    ContributorAuthor
    More actions

    @normanmaurer I tried it out and unfortunately it seems I was right.
    As long as the classloader is still referenced - in this case by the io.netty.resolver.dns.macos.DnsResolver class that was loaded by the classloader itself and is registered as a JNI global reference - the native library will not be unloaded and so the JNI global reference will not be freed.
    This means that the calls to NETTY_JNI_UTIL_UNLOAD_CLASS inside a JNI_OnUnLoad function are basically useless as they will never be executed.
    This basically means that netty can not really be used in a JVM that uses some sort of plugin system that might want to create and release new classloaders dynamically (for example gradle), which is very unfortunate. JVM Metaspace will monotonically increase.

    I guess I should open a new issue, right?

  13. normanmaurer commented on Jul 12, 2023

    @normanmaurer
    normanmaurer
    on Jul 12, 2023 via email
    Member
    More actions
    Yes … that said I am not really sure what to do about atm Am 12.07.2023 um 15:53 schrieb Silvio Giebl ***@***.***>: @normanmaurer I tried it out and unfortunately it seems I was right. As long as the classloader is still referenced - in this case by the io.netty.resolver.dns.macos.DnsResolver class that was loaded by the classloader itself and is registered as a JNI global reference - the native library will not be unloaded and so the JNI global reference will not be freed. This means that the calls to NETTY_JNI_UTIL_UNLOAD_CLASS inside a JNI_OnUnLoad class are basically useless as they will never be executed. This basically means that netty can not really be used in a JVM that uses some sort of plugin system that might want to create and release new classloaders dynamically (for example gradle) which is very unfortunate. I guess I should open a new issue, right? —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
  14. SgtSilvio commented on Jul 12, 2023

    @SgtSilvio
    SgtSilvio
    on Jul 12, 2023
    ContributorAuthor
    More actions

    Maybe using weak global references instead of global references of the classes is a solution.

    What do you think about this? It seems like using a weak global reference might be right in general for references to classes. Because, as long as the classloader is referenced from at least one place in the JVM, the weak JNI reference will stay there as well. (as of my current understanding)

  15. normanmaurer commented on Jul 12, 2023

    @normanmaurer
    normanmaurer
    on Jul 12, 2023
    Member
    More actions

    This "might" work. Can you patch it and test it ? If the maven command which I shared with you did not work, please show the full output. Most likely you miss the tools that are needed to compile native stuff:

    https://netty.io/wiki/native-transports.html#wiki-h2-4

  16. 33 remaining items

  17. normanmaurer
    added a commit that references this issue on Sep 4, 2023

    Prevent classloader leak via JNI

    4771d64
  18. normanmaurer
    closed this as completedin #13584on Sep 5, 2023
  19. normanmaurer
    self-assigned this
    on Sep 5, 2023
  20. normanmaurer
    added 6 commits that reference this issue on Sep 5, 2023

    Prevent classloader leak via JNI (#13584)

    ea801cf

    Prevent JNI classloader leak (#574)

    487fcb2

    Prevent classloader leak via JNI (#817)

    040e305

    Prevent classloader leak via JNI

    309cf1b

    Prevent classloader leak via JNI

    d01e6d2

    Prevent classloader leak via JNI (#13586)

    e133ed3
  21. normanmaurer
    added 4 commits that reference this issue on Sep 26, 2023

    Do not try to delete a global handle with the local handles APIs (#588)

    b035ef8

    Do not try to delete a global handle with the local handles APIs (#588)

    144282d

    Do not try to delete a global handle with the local handles APIs (#13640

    2fcda07

    Do not try to delete a global handle with the local handles APIs (#13640

    db71720
  22. agilob commented on Feb 12, 2024

    @agilob
    agilob
    on Feb 12, 2024
    More actions

    Sorry to bring this topic to life again, but how can I tell if I'm affected by this leak? I've been chasing a native memory leak for a few weeks now, now I'm looking into netty-transport-native-epoll-4.1.97. I have access to a few heapdump, jfr recordings, and jemalloc snapshots.

    For the first point, I want to mention that GC of classloaders seems to be very lazy and I see it delayed by several minutes, but heapdumps show that the classloader actually has no GC root, and this really fixes the issue.

    Would I be looking for classloader objects that simply have no GC root?

  23. SgtSilvio commented on Feb 12, 2024

    @SgtSilvio
    SgtSilvio
    on Feb 12, 2024
    ContributorAuthor
    More actions

    @agilob this issue has been fixed since 4.1.98, so it is possible that you hit it as you are still on 4.1.97.
    But it depends on what your application does. This issue only happens if you create multiple classloaders that are then later unloaded.

    Would I be looking for classloader objects that simply have no GC root?

    No, you should see a JNI global reference as a GC root. The following screenshot shows how this can look like in VisualVM:

    image
    Reacted by agilob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @normanmaurer
    normanmaurer

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    • 4.1.98.Final

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.