Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class GCSubscriber {
ConcurrentHashMap<PhantomReference<Object>, Integer> referencesMap = new ConcurrentHashMap<>();
public final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
boolean unsubscribed; // guarded by this
}

public class GcListener {
Expand Down Expand Up @@ -60,7 +62,11 @@ public void run() {
for (int i =0; i < objIds.length; i++) {
objIds[i] = collectedObjectIds.get(i);
}
runtime.notifyGc(objIds);
synchronized (subscriber) {
if (!subscriber.unsubscribed) {
runtime.notifyGc(objIds);
}
}
if (runtime.getLogger().isEnabled()) {
runtime.getLogger().write("GC", "GC triggered for " + String.valueOf(objIds.length) + " objects");
runtime.getLogger().write("NS.GCListener","Objects referenced after GC: " + subscriber.referencesMap.size());
Expand Down Expand Up @@ -152,9 +158,17 @@ public static void subscribe(Runtime runtime) {
}
}

/**
* Once this returns, no GC thread is inside the runtime's notifyGc and none
* will enter it. Must not be called from inside that runtime's JS scope:
* delivery holds the subscriber's lock while it waits to enter the runtime.
*/
public static void unsubscribe(Runtime runtime) {
synchronized (instance) {
instance.subscribers.remove(runtime);
GCSubscriber subscriber = instance.subscribers.remove(runtime);
if (subscriber != null) {
synchronized (subscriber) {
subscriber.unsubscribed = true;
}
}
}

Expand All @@ -168,15 +182,18 @@ private void start() {
}

private void notifyGc() {
synchronized (instance) {
// ManualInstrumentation.Frame frame = ManualInstrumentation.start("GcListener.notifyGc");
try {
for (Runtime runtime : instance.subscribers.keySet()) {
runtime.notifyGc();
// ManualInstrumentation.Frame frame = ManualInstrumentation.start("GcListener.notifyGc");
try {
for (Map.Entry<Runtime, GCSubscriber> entry : subscribers.entrySet()) {
GCSubscriber subscriber = entry.getValue();
synchronized (subscriber) {
if (!subscriber.unsubscribed) {
entry.getKey().notifyGc();
}
}
} finally {
// frame.close();
}
} finally {
// frame.close();
}
}
}
Loading