Skip to content

Commit 369eae9

Browse files
committed
fixup! Fix ChannelFetcher memory leak
1 parent e3fbff1 commit 369eae9

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class TelemetryClient {
5454
private volatile TelemetryContext context;
5555
private TelemetryChannel channel;
5656

57+
private static final Object TELEMETRY_STOP_HOOK_LOCK = new Object();
5758
private static final Object TELEMETRY_CONTEXT_LOCK = new Object();
5859

5960
private static AtomicLong generateCounter = new AtomicLong(0);
@@ -66,6 +67,10 @@ public TelemetryClient(TelemetryConfiguration configuration) {
6667
configuration = TelemetryConfiguration.getActive();
6768
}
6869

70+
synchronized (TELEMETRY_STOP_HOOK_LOCK) {
71+
SDKShutdownActivity.INSTANCE.register(configuration.getChannel());
72+
}
73+
6974
this.configuration = configuration;
7075
}
7176

@@ -497,7 +502,6 @@ public void flush() {
497502
TelemetryChannel getChannel() {
498503
if (this.channel == null) {
499504
this.channel = configuration.getChannel();
500-
SDKShutdownActivity.INSTANCE.register(this.channel);
501505
}
502506

503507
return this.channel;

core/src/main/java/com/microsoft/applicationinsights/internal/shutdown/SDKShutdownActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.microsoft.applicationinsights.internal.shutdown;
2323

2424
import java.io.Closeable;
25+
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.ArrayList;
2728
import java.util.Map;
@@ -53,7 +54,7 @@ public enum SDKShutdownActivity {
5354
private static class SDKShutdownAction implements Runnable {
5455
private boolean stopped = false;
5556

56-
private final Map<TelemetryChannel, Boolean> channels = new WeakHashMap<>();
57+
private final Map<TelemetryChannel, Boolean> channels = new HashMap<>();
5758
@Deprecated
5859
private final List<ChannelFetcher> fetchers = new ArrayList<ChannelFetcher>();
5960
private final List<Stoppable> stoppables = new ArrayList<Stoppable>();
@@ -151,8 +152,9 @@ private void stopChannel(TelemetryChannel channelToStop) {
151152
throw td;
152153
} catch (Throwable t) {
153154
try {
154-
InternalLogger.INSTANCE.error("Failed to stop channel: '%s'", t.toString());
155-
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(t));
155+
if (InternalLogger.INSTANCE.isErrorEnabled()) {
156+
InternalLogger.INSTANCE.error("Failed to stop channel: '%s'", ExceptionUtils.getStackTrace(t));
157+
}
156158
} catch (ThreadDeath td) {
157159
throw td;
158160
} catch (Throwable t2) {

0 commit comments

Comments
 (0)