GeezerVison is showing github.com — Classloader memory leak via JNI global references · Issue #13480 · netty/netty
Classloader memory leak via JNI global references #13480DescriptionExpected 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_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 Activitynormanmaurer commented on Jul 10, 2023 I think the problem is that we don't call NETTY_JNI_UTIL_UNLOAD_CLASS for it ... Let me fix this normanmaurer commented on Jul 10, 2023 SgtSilvio commented 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). normanmaurer commented on Jul 11, 2023 @SgtSilvio I think it should work, that said I might also be wrong :) SgtSilvio commented 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. normanmaurer commented on Jul 11, 2023 @SgtSilvio this should work:
./mvnw -pl transport-native-unix-common,resolver-dns-native-macos,resolver-dns-classes-macos clean package
SgtSilvio commented 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 SgtSilvio commented 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? normanmaurer commented on Jul 12, 2023 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: ***@***.***> SgtSilvio commented 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) normanmaurer commented on Jul 12, 2023 agilob commented on Feb 12, 2024 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? SgtSilvio commented 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:

MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|