mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
cleanup due to low memory on some other thread
This commit is contained in:
@@ -25,6 +25,10 @@ import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.MemoryNotificationInfo;
|
||||
import java.lang.management.MemoryPoolMXBean;
|
||||
import java.lang.management.MemoryType;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Eugene Zhuravlev
|
||||
@@ -36,6 +40,34 @@ public class LowMemoryWatcher {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.LowMemoryWatcher");
|
||||
|
||||
private static final WeakList<LowMemoryWatcher> ourInstances = new WeakList<LowMemoryWatcher>();
|
||||
private static final ThreadPoolExecutor ourExecutor = new ThreadPoolExecutor(0, 1, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), new ThreadFactory() {
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
return new Thread(r, "LowMemoryWatcher janitor");
|
||||
}
|
||||
});
|
||||
private static boolean ourSubmitted;
|
||||
private static final Runnable ourJanitor = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
for (LowMemoryWatcher watcher : ourInstances) {
|
||||
try {
|
||||
watcher.myRunnable.run();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
synchronized (ourJanitor) {
|
||||
//noinspection AssignmentToStaticFieldFromInstanceMethod
|
||||
ourSubmitted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final Runnable myRunnable;
|
||||
|
||||
@@ -52,12 +84,11 @@ public class LowMemoryWatcher {
|
||||
((NotificationEmitter)ManagementFactory.getMemoryMXBean()).addNotificationListener(new NotificationListener() {
|
||||
public void handleNotification(Notification n, Object hb) {
|
||||
if (MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED.equals(n.getType()) || MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED.equals(n.getType())) {
|
||||
for (LowMemoryWatcher watcher : ourInstances) {
|
||||
try {
|
||||
watcher.myRunnable.run();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
synchronized (ourJanitor) {
|
||||
if (!ourSubmitted) {
|
||||
//noinspection AssignmentToStaticFieldFromInstanceMethod
|
||||
ourSubmitted = true;
|
||||
ourExecutor.submit(ourJanitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user