jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 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 | |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 17 | import java.util.concurrent.CountDownLatch; |
| 18 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 19 | public class Main { |
| 20 | Bitmap mBitmap1, mBitmap2, mBitmap3, mBitmap4; |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 21 | CountDownLatch mFreeSignalA, mFreeSignalB; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 22 | |
| 23 | public static void sleep(int ms) { |
| 24 | try { |
| 25 | Thread.sleep(ms); |
| 26 | } catch (InterruptedException ie) { |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 27 | System.out.println("sleep interrupted"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
| 31 | public static void main(String args[]) { |
| 32 | System.out.println("start"); |
| 33 | |
| 34 | Main main = new Main(); |
| 35 | main.run(); |
| 36 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 37 | System.out.println("done"); |
| 38 | } |
| 39 | |
| 40 | public void run() { |
| 41 | createBitmaps(); |
| 42 | |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 43 | Runtime.getRuntime().gc(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 44 | sleep(250); |
| 45 | |
| 46 | mBitmap2.drawAt(0, 0); |
| 47 | |
| 48 | System.out.println("nulling 1"); |
| 49 | mBitmap1 = null; |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 50 | Runtime.getRuntime().gc(); |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 51 | try { |
| 52 | mFreeSignalA.await(); // Block until dataA is definitely freed. |
| 53 | } catch (InterruptedException e) { |
| 54 | System.out.println("got unexpected InterruptedException e: " + e); |
| 55 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 56 | |
| 57 | System.out.println("nulling 2"); |
| 58 | mBitmap2 = null; |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 59 | Runtime.getRuntime().gc(); |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 60 | sleep(200); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 61 | |
| 62 | System.out.println("nulling 3"); |
| 63 | mBitmap3 = null; |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 64 | Runtime.getRuntime().gc(); |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 65 | sleep(200); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 66 | |
| 67 | System.out.println("nulling 4"); |
| 68 | mBitmap4 = null; |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 69 | Runtime.getRuntime().gc(); |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 70 | try { |
| 71 | mFreeSignalB.await(); // Block until dataB is definitely freed. |
| 72 | } catch (InterruptedException e) { |
| 73 | System.out.println("got unexpected InterruptedException e: " + e); |
| 74 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 75 | |
| 76 | Bitmap.shutDown(); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Create bitmaps. |
| 81 | * |
| 82 | * bitmap1 is 10x10 and unique |
| 83 | * bitmap2 and bitmap3 are 20x20 and share the same storage. |
| 84 | * bitmap4 is just another reference to bitmap3 |
| 85 | * |
| 86 | * When we return there should be no local refs lurking on the stack. |
| 87 | */ |
| 88 | public void createBitmaps() { |
| 89 | Bitmap.NativeWrapper dataA = Bitmap.allocNativeStorage(10, 10); |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 90 | mFreeSignalA = dataA.mPhantomWrapper.mFreeSignal; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 91 | Bitmap.NativeWrapper dataB = Bitmap.allocNativeStorage(20, 20); |
Igor Murashkin | 2b592ab | 2017-07-06 13:16:34 -0700 | [diff] [blame] | 92 | mFreeSignalB = dataB.mPhantomWrapper.mFreeSignal; |
| 93 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 94 | mBitmap1 = new Bitmap("one", 10, 10, dataA); |
| 95 | mBitmap2 = new Bitmap("two", 20, 20, dataB); |
| 96 | mBitmap3 = mBitmap4 = new Bitmap("three/four", 20, 20, dataB); |
| 97 | } |
| 98 | } |