|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.archive.schedulers; |
| 17 | + |
| 18 | +import rx.Observable; |
| 19 | +import rx.Observable.OnSubscribe; |
| 20 | +import rx.Scheduler; |
| 21 | +import rx.Scheduler.Inner; |
| 22 | +import rx.Subscriber; |
| 23 | +import rx.functions.Action1; |
| 24 | +import rx.schedulers.Schedulers; |
| 25 | + |
| 26 | +/** |
| 27 | + * Used for manual testing of memory leaks with recursive schedulers. |
| 28 | + * |
| 29 | + */ |
| 30 | +public class TestRecursionMemoryUsage { |
| 31 | + |
| 32 | + public static void main(String args[]) { |
| 33 | + usingFunc2(Schedulers.newThread()); |
| 34 | + usingAction0(Schedulers.newThread()); |
| 35 | + |
| 36 | + usingFunc2(Schedulers.currentThread()); |
| 37 | + usingAction0(Schedulers.currentThread()); |
| 38 | + |
| 39 | + usingFunc2(Schedulers.computation()); |
| 40 | + usingAction0(Schedulers.computation()); |
| 41 | + |
| 42 | + System.exit(0); |
| 43 | + } |
| 44 | + |
| 45 | + protected static void usingFunc2(final Scheduler scheduler) { |
| 46 | + System.out.println("************ usingFunc2: " + scheduler); |
| 47 | + Observable.create(new OnSubscribe<Long>() { |
| 48 | + |
| 49 | + @Override |
| 50 | + public void call(final Subscriber<? super Long> o) { |
| 51 | + o.add(scheduler.schedule(new Action1<Inner>() { |
| 52 | + long i = 0; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void call(Inner inner) { |
| 56 | + i++; |
| 57 | + if (i % 500000 == 0) { |
| 58 | + System.out.println(i + " Total Memory: " |
| 59 | + + Runtime.getRuntime().totalMemory() |
| 60 | + + " Free: " |
| 61 | + + Runtime.getRuntime().freeMemory()); |
| 62 | + o.onNext(i); |
| 63 | + } |
| 64 | + if (i == 100000000L) { |
| 65 | + o.onCompleted(); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + inner.schedule(this); |
| 70 | + } |
| 71 | + })); |
| 72 | + } |
| 73 | + }).toBlockingObservable().last(); |
| 74 | + } |
| 75 | + |
| 76 | + protected static void usingAction0(final Scheduler scheduler) { |
| 77 | + System.out.println("************ usingAction0: " + scheduler); |
| 78 | + Observable.create(new OnSubscribe<Long>() { |
| 79 | + |
| 80 | + @Override |
| 81 | + public void call(final Subscriber<? super Long> o) { |
| 82 | + o.add(scheduler.schedule(new Action1<Inner>() { |
| 83 | + |
| 84 | + private long i = 0; |
| 85 | + |
| 86 | + @Override |
| 87 | + public void call(Inner inner) { |
| 88 | + i++; |
| 89 | + if (i % 500000 == 0) { |
| 90 | + System.out.println(i + " Total Memory: " |
| 91 | + + Runtime.getRuntime().totalMemory() |
| 92 | + + " Free: " |
| 93 | + + Runtime.getRuntime().freeMemory()); |
| 94 | + o.onNext(i); |
| 95 | + } |
| 96 | + if (i == 100000000L) { |
| 97 | + o.onCompleted(); |
| 98 | + return; |
| 99 | + } |
| 100 | + inner.schedule(this); |
| 101 | + } |
| 102 | + })); |
| 103 | + } |
| 104 | + }).toBlockingObservable().last(); |
| 105 | + } |
| 106 | +} |
0 commit comments