GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing github.com — RecordingAPI JNI global reference prevents class loader unloading · Issue #1811 · async-profiler/async-profiler Closed alibaba/arthas #3281DescriptionDescribe the bugLoading async-profiler through a disposable Calling I reproduced this with the official 4.5 release and current master at This affects applications that load the profiler into a disposable class loader, such as reloadable plugins: the loader, its loaded classes/static state, and its native library cannot be unloaded. Expected vs. actual behaviorExpected: Once the helper method below returns, there is no active profiling session and the caller holds only a Actual: The loader remains reachable after 30 explicit full-GC/finalization attempts. The same program collects the loader when native loading is skipped, or when Reproduction StepsThe reproducer below is a single Java file. It uses only JDK classes and upstream async-profiler artifacts; no build tool, framework, agent attachment, or additional dependency is required. Save as import java.io.File; import java.lang.ref.WeakReference; import java.net.URL; import java.net.URLClassLoader; public class ProfilerClassLoaderLeak { private static WeakReference<ClassLoader> loadAndRelease(String[] args) throws Exception { URL apiJar = new File(args[0]).toURI().toURL(); URLClassLoader loader = new URLClassLoader(new URL[] {apiJar}, null); try { Class<?> api = Class.forName("one.profiler.AsyncProfiler", true, loader); String mode = args.length == 3 ? args[2] : "version"; if (!"no-native".equals(mode)) { Object profiler = api.getMethod("getInstance", String.class) .invoke(null, new File(args[1]).getAbsolutePath()); System.out.println("version=" + api.getMethod("getVersion").invoke(profiler)); if ("start-stop".equals(mode)) { System.out.print(api.getMethod("execute", String.class) .invoke(profiler, "start,event=cpu")); Thread.sleep(100); api.getMethod("stop").invoke(profiler); System.out.print(api.getMethod("execute", String.class).invoke(profiler, "status")); } } return new WeakReference<ClassLoader>(loader); } finally { loader.close(); } } public static void main(String[] args) throws Exception { if (args.length < 2 || args.length > 3 || (args.length == 3 && !"version".equals(args[2]) && !"no-native".equals(args[2]) && !"start-stop".equals(args[2]))) { throw new IllegalArgumentException( "Usage: java ProfilerClassLoaderLeak <api.jar> <native-library> [version|no-native|start-stop]"); } WeakReference<ClassLoader> loader = loadAndRelease(args); for (int i = 0; i < 30 && loader.get() != null; i++) { System.gc(); System.runFinalization(); Thread.sleep(100); } System.out.println("classLoaderCollected=" + (loader.get() == null)); } } Download the official 4.5 artifacts and run on macOS: curl -fLO https://github.com/async-profiler/async-profiler/releases/download/v4.5/async-profiler.jar
curl -fLO https://github.com/async-profiler/async-profiler/releases/download/v4.5/async-profiler-4.5-macos.zip
unzip -q async-profiler-4.5-macos.zip
javac ProfilerClassLoaderLeak.java
java -Xint -XX:+UseSerialGC -cp . ProfilerClassLoaderLeak \
async-profiler.jar \
async-profiler-4.5-macos/lib/libasyncProfiler.dylibThe main application class path is just Actual output on both tested JDKs: The source also accepts a Linux To run the identical reproducer against the latest tested source, build it with JDK 11 or newer: git clone https://github.com/async-profiler/async-profiler.git async-profiler-src
git -C async-profiler-src checkout a725243e50f55cdbffc60bc6920ede281192f78a
make -C async-profiler-src -j4 COMMIT_TAG=true
java -Xint -XX:+UseSerialGC -cp . ProfilerClassLoaderLeak \
async-profiler-src/build/jar/async-profiler.jar \
async-profiler-src/build/lib/libasyncProfiler.dylibOutput: Controls, using that same master build: # Skip native loading; the same URLClassLoader is collected. java -Xint -XX:+UseSerialGC -cp . ProfilerClassLoaderLeak \ async-profiler-src/build/jar/async-profiler.jar \ async-profiler-src/build/lib/libasyncProfiler.dylib no-native # Explicitly stop a real profiling session; the loader is still retained. java -Xint -XX:+UseSerialGC -cp . ProfilerClassLoaderLeak \ async-profiler-src/build/jar/async-profiler.jar \ async-profiler-src/build/lib/libasyncProfiler.dylib start-stop # Remove only the Span API classes from a separate copy of the jar. cp async-profiler-src/build/jar/async-profiler.jar api-without-span.jar zip -d api-without-span.jar 'one/profiler/Recording.class' 'one/profiler/Span*.class' java -Xint -XX:+UseSerialGC -cp . ProfilerClassLoaderLeak \ api-without-span.jar \ async-profiler-src/build/lib/libasyncProfiler.dylib Results with the standalone program:
I also compiled and ran the unmodified source in a clean temporary directory containing only this Java file and the official API/native artifacts. Additional Information/ContextThe retaining reference is in _recording_class = (jclass) env->NewGlobalRef(recording_class);The load path reaches it automatically:
The reference originates from the Span API introduced in #1755. I checked existing issues; #1764 concerns repeated method retransformation and lambda classes, while this reproducer only queries the profiler version. Async-profiler version
Environment details
Activity
MetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue 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. |