Skip to content

Commit

Permalink
Merge pull request #1922 from akarnokd/SchedulersRemoveOnCancelPolicy
Browse files Browse the repository at this point in the history
Set removeOnCancelPolicy on the threadpool if supported
  • Loading branch information
benjchristensen committed Dec 9, 2014
2 parents 57156e3 + 87ea431 commit 6620a14
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/main/java/rx/internal/schedulers/NewThreadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package rx.internal.schedulers;

import rx.Scheduler;
import rx.Subscription;
import java.lang.reflect.Method;
import java.util.concurrent.*;

import rx.*;
import rx.functions.Action0;
import rx.plugins.RxJavaPlugins;
import rx.plugins.RxJavaSchedulersHook;
import rx.plugins.*;
import rx.subscriptions.Subscriptions;

import java.util.concurrent.*;

/**
* @warn class description missing
*/
Expand All @@ -35,6 +34,19 @@ public class NewThreadWorker extends Scheduler.Worker implements Subscription {
/* package */
public NewThreadWorker(ThreadFactory threadFactory) {
executor = Executors.newScheduledThreadPool(1, threadFactory);
// Java 7+: cancelled future tasks can be removed from the executor thus avoiding memory leak
for (Method m : executor.getClass().getMethods()) {
if (m.getName().equals("setRemoveOnCancelPolicy")
&& m.getParameterTypes().length == 1
&& m.getParameterTypes()[0] == Boolean.TYPE) {
try {
m.invoke(executor, true);
} catch (Exception ex) {
RxJavaPlugins.getInstance().getErrorHandler().handleError(ex);
}
break;
}
}
schedulersHook = RxJavaPlugins.getInstance().getSchedulersHook();
}

Expand Down
65 changes: 62 additions & 3 deletions src/test/java/rx/schedulers/CachedThreadSchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

package rx.schedulers;

import java.lang.management.*;
import java.util.concurrent.*;

import junit.framework.Assert;

import org.junit.Test;

import rx.Observable;
import rx.Scheduler;
import rx.functions.Action1;
import rx.functions.Func1;

import rx.functions.*;
import static org.junit.Assert.assertTrue;

public class CachedThreadSchedulerTest extends AbstractSchedulerConcurrencyTests {
Expand Down Expand Up @@ -66,4 +70,59 @@ public final void testUnhandledErrorIsDeliveredToThreadHandler() throws Interrup
public final void testHandledErrorIsNotDeliveredToThreadHandler() throws InterruptedException {
SchedulerTests.testHandledErrorIsNotDeliveredToThreadHandler(getScheduler());
}

@Test(timeout = 30000)
public void testCancelledTaskRetention() throws InterruptedException {
try {
ScheduledThreadPoolExecutor.class.getMethod("setRemoveOnCancelPolicy", Boolean.TYPE);

System.out.println("Wait before GC");
Thread.sleep(1000);

System.out.println("GC");
System.gc();

Thread.sleep(1000);


MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage();
long initial = memHeap.getUsed();

System.out.printf("Starting: %.3f MB%n", initial / 1024.0 / 1024.0);

Scheduler.Worker w = Schedulers.io().createWorker();
for (int i = 0; i < 750000; i++) {
if (i % 50000 == 0) {
System.out.println(" -> still scheduling: " + i);
}
w.schedule(Actions.empty(), 1, TimeUnit.DAYS);
}

memHeap = memoryMXBean.getHeapMemoryUsage();
long after = memHeap.getUsed();
System.out.printf("Peak: %.3f MB%n", after / 1024.0 / 1024.0);

w.unsubscribe();

System.out.println("Wait before second GC");
Thread.sleep(1000);

System.out.println("Second GC");
System.gc();

Thread.sleep(1000);

memHeap = memoryMXBean.getHeapMemoryUsage();
long finish = memHeap.getUsed();
System.out.printf("After: %.3f MB%n", finish / 1024.0 / 1024.0);

if (finish > initial * 5) {
Assert.fail(String.format("Tasks retained: %.3f -> %.3f -> %.3f", initial / 1024 / 1024.0, after / 1024 / 1024.0, finish / 1024 / 1024d));
}
} catch (NoSuchMethodException ex) {
// not supported, no reason to test for it
}
}

}

0 comments on commit 6620a14

Please sign in to comment.