forked from I2P_Developers/i2p.i2p
Register shutdown hooks for caches, in the first step of cleaning up
resources on shutdown, which is important in Android as the JVM isn't going away. More to do.
This commit is contained in:
@ -83,6 +83,18 @@ public class SDSCache<V extends SimpleDataStructure> {
|
|||||||
_log.debug("New SDSCache for " + rvClass + " data size: " + len +
|
_log.debug("New SDSCache for " + rvClass + " data size: " + len +
|
||||||
" max: " + size + " max mem: " + (len * size));
|
" max: " + size + " max mem: " + (len * size));
|
||||||
I2PAppContext.getGlobalContext().statManager().createRateStat(_statName, "Hit rate", "Router", new long[] { 10*60*1000 });
|
I2PAppContext.getGlobalContext().statManager().createRateStat(_statName, "Hit rate", "Router", new long[] { 10*60*1000 });
|
||||||
|
I2PAppContext.getGlobalContext().addShutdownTask(new Shutdown());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
private class Shutdown implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
synchronized(_cache) {
|
||||||
|
_cache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,8 +107,18 @@ public class DecayingBloomFilter {
|
|||||||
context.statManager().createRateStat("router.decayingBloomFilter." + name + ".log10(falsePos)",
|
context.statManager().createRateStat("router.decayingBloomFilter." + name + ".log10(falsePos)",
|
||||||
"log10 of the false positive rate (must have net.i2p.util.DecayingBloomFilter=DEBUG)",
|
"log10 of the false positive rate (must have net.i2p.util.DecayingBloomFilter=DEBUG)",
|
||||||
"Router", new long[] { Math.max(60*1000, durationMs) });
|
"Router", new long[] { Math.max(60*1000, durationMs) });
|
||||||
|
context.addShutdownTask(new Shutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
private class Shutdown implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public long getCurrentDuplicateCount() { return _currentDuplicates; }
|
public long getCurrentDuplicateCount() { return _currentDuplicates; }
|
||||||
|
|
||||||
public int getInsertedCount() {
|
public int getInsertedCount() {
|
||||||
|
@ -93,8 +93,18 @@ public class DecayingHashSet extends DecayingBloomFilter {
|
|||||||
"Size", "Router", new long[] { Math.max(60*1000, durationMs) });
|
"Size", "Router", new long[] { Math.max(60*1000, durationMs) });
|
||||||
context.statManager().createRateStat("router.decayingHashSet." + name + ".dups",
|
context.statManager().createRateStat("router.decayingHashSet." + name + ".dups",
|
||||||
"1000000 * Duplicates/Size", "Router", new long[] { Math.max(60*1000, durationMs) });
|
"1000000 * Duplicates/Size", "Router", new long[] { Math.max(60*1000, durationMs) });
|
||||||
|
context.addShutdownTask(new Shutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
private class Shutdown implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** unsynchronized but only used for logging elsewhere */
|
/** unsynchronized but only used for logging elsewhere */
|
||||||
@Override
|
@Override
|
||||||
public int getInsertedCount() {
|
public int getInsertedCount() {
|
||||||
|
@ -2,6 +2,7 @@ package net.i2p.util;
|
|||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
|
||||||
@ -48,12 +49,25 @@ public class SimpleScheduler {
|
|||||||
_threads = (int) Math.max(MIN_THREADS, Math.min(MAX_THREADS, 1 + (maxMemory / (32*1024*1024))));
|
_threads = (int) Math.max(MIN_THREADS, Math.min(MAX_THREADS, 1 + (maxMemory / (32*1024*1024))));
|
||||||
_executor = new ScheduledThreadPoolExecutor(_threads, new CustomThreadFactory());
|
_executor = new ScheduledThreadPoolExecutor(_threads, new CustomThreadFactory());
|
||||||
_executor.prestartAllCoreThreads();
|
_executor.prestartAllCoreThreads();
|
||||||
|
// don't bother saving ref to remove hook if somebody else calls stop
|
||||||
|
_context.addShutdownTask(new Shutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the SimpleScheduler.
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
private class Shutdown implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the SimpleScheduler.
|
||||||
|
* Subsequent executions should not throw a RejectedExecutionException.
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
_executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
|
||||||
_executor.shutdownNow();
|
_executor.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,16 +53,32 @@ public class SimpleTimer {
|
|||||||
executor.setDaemon(true);
|
executor.setDaemon(true);
|
||||||
executor.start();
|
executor.start();
|
||||||
}
|
}
|
||||||
|
_context.addShutdownTask(new Shutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
private class Shutdown implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
removeSimpleTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the SimpleTimer.
|
* Removes the SimpleTimer.
|
||||||
*/
|
*/
|
||||||
public void removeSimpleTimer() {
|
public void removeSimpleTimer() {
|
||||||
synchronized(_events) {
|
synchronized(_events) {
|
||||||
runn.setAnswer(false);
|
runn.setAnswer(false);
|
||||||
|
_events.clear();
|
||||||
|
_eventTimes.clear();
|
||||||
_events.notifyAll();
|
_events.notifyAll();
|
||||||
}
|
}
|
||||||
|
synchronized (_readyEvents) {
|
||||||
|
_readyEvents.clear();
|
||||||
|
_readyEvents.notifyAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ package net.i2p.util;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
|
||||||
@ -48,12 +49,25 @@ public class SimpleTimer2 {
|
|||||||
_threads = (int) Math.max(MIN_THREADS, Math.min(MAX_THREADS, 1 + (maxMemory / (32*1024*1024))));
|
_threads = (int) Math.max(MIN_THREADS, Math.min(MAX_THREADS, 1 + (maxMemory / (32*1024*1024))));
|
||||||
_executor = new CustomScheduledThreadPoolExecutor(_threads, new CustomThreadFactory());
|
_executor = new CustomScheduledThreadPoolExecutor(_threads, new CustomThreadFactory());
|
||||||
_executor.prestartAllCoreThreads();
|
_executor.prestartAllCoreThreads();
|
||||||
|
// don't bother saving ref to remove hook if somebody else calls stop
|
||||||
|
_context.addShutdownTask(new Shutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the SimpleTimer.
|
* @since 0.8.8
|
||||||
|
*/
|
||||||
|
private class Shutdown implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the SimpleTimer.
|
||||||
|
* Subsequent executions should not throw a RejectedExecutionException.
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
_executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
|
||||||
_executor.shutdownNow();
|
_executor.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user