|
| 1 | +package rx.schedulers; |
| 2 | + |
| 3 | +import rx.Observable; |
| 4 | +import rx.Observable.OnSubscribeFunc; |
| 5 | +import rx.Observer; |
| 6 | +import rx.Scheduler; |
| 7 | +import rx.Subscription; |
| 8 | +import rx.subscriptions.Subscriptions; |
| 9 | +import rx.util.functions.Action0; |
| 10 | +import rx.util.functions.Action1; |
| 11 | +import rx.util.functions.Func2; |
| 12 | + |
| 13 | +/** |
| 14 | + * Used for manual testing of memory leaks with recursive schedulers. |
| 15 | + * |
| 16 | + */ |
| 17 | +public class TestRecursionMemoryUsage { |
| 18 | + |
| 19 | + public static void main(String args[]) { |
| 20 | + usingFunc2(Schedulers.newThread()); |
| 21 | + usingAction0(Schedulers.newThread()); |
| 22 | +// |
| 23 | +// usingFunc2(Schedulers.currentThread()); |
| 24 | +// usingAction0(Schedulers.currentThread()); |
| 25 | + |
| 26 | +// usingFunc2(Schedulers.threadPoolForComputation()); |
| 27 | +// usingAction0(Schedulers.threadPoolForComputation()); |
| 28 | + } |
| 29 | + |
| 30 | + protected static void usingFunc2(final Scheduler scheduler) { |
| 31 | + System.out.println("************ usingFunc2: " + scheduler); |
| 32 | + Observable.create(new OnSubscribeFunc<Long>() { |
| 33 | + |
| 34 | + @Override |
| 35 | + public Subscription onSubscribe(final Observer<? super Long> o) { |
| 36 | + return scheduler.schedule(0L, new Func2<Scheduler, Long, Subscription>() { |
| 37 | + |
| 38 | + @Override |
| 39 | + public Subscription call(Scheduler innerScheduler, Long i) { |
| 40 | + i++; |
| 41 | + if (i % 500000 == 0) { |
| 42 | + System.out.println(i + " Total Memory: " + Runtime.getRuntime().totalMemory() + " Free: " + Runtime.getRuntime().freeMemory()); |
| 43 | + o.onNext(i); |
| 44 | + } |
| 45 | + if (i == 100000000L) { |
| 46 | + o.onCompleted(); |
| 47 | + return Subscriptions.empty(); |
| 48 | + } |
| 49 | + |
| 50 | + return innerScheduler.schedule(i, this); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + }).toBlockingObservable().last(); |
| 55 | + } |
| 56 | + |
| 57 | + protected static void usingAction0(final Scheduler scheduler) { |
| 58 | + System.out.println("************ usingAction0: " + scheduler); |
| 59 | + Observable.create(new OnSubscribeFunc<Long>() { |
| 60 | + |
| 61 | + @Override |
| 62 | + public Subscription onSubscribe(final Observer<? super Long> o) { |
| 63 | + return scheduler.schedule(new Action1<Action0>() { |
| 64 | + |
| 65 | + private long i = 0; |
| 66 | + |
| 67 | + @Override |
| 68 | + public void call(Action0 self) { |
| 69 | + i++; |
| 70 | + if (i % 500000 == 0) { |
| 71 | + System.out.println(i + " Total Memory: " + Runtime.getRuntime().totalMemory() + " Free: " + Runtime.getRuntime().freeMemory()); |
| 72 | + o.onNext(i); |
| 73 | + } |
| 74 | + if (i == 100000000L) { |
| 75 | + o.onCompleted(); |
| 76 | + return; |
| 77 | + } |
| 78 | + self.call(); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + }).toBlockingObservable().last(); |
| 83 | + } |
| 84 | +} |
0 commit comments