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 
17 import java.util.concurrent.CountDownLatch;
18 
19 public class Main {
20     Bitmap mBitmap1, mBitmap2, mBitmap3, mBitmap4;
21     CountDownLatch mFreeSignalA, mFreeSignalB;
22 
sleep(int ms)23     public static void sleep(int ms) {
24         try {
25             Thread.sleep(ms);
26         } catch (InterruptedException ie) {
27             System.out.println("sleep interrupted");
28         }
29     }
30 
main(String args[])31     public static void main(String args[]) {
32         System.out.println("start");
33 
34         Main main = new Main();
35         main.run();
36 
37         System.out.println("done");
38     }
39 
run()40     public void run() {
41         createBitmaps();
42 
43         Runtime.getRuntime().gc();
44         sleep(250);
45 
46         mBitmap2.drawAt(0, 0);
47 
48         System.out.println("nulling 1");
49         mBitmap1 = null;
50         Runtime.getRuntime().gc();
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         }
56 
57         System.out.println("nulling 2");
58         mBitmap2 = null;
59         Runtime.getRuntime().gc();
60         sleep(200);
61 
62         System.out.println("nulling 3");
63         mBitmap3 = null;
64         Runtime.getRuntime().gc();
65         sleep(200);
66 
67         System.out.println("nulling 4");
68         mBitmap4 = null;
69         Runtime.getRuntime().gc();
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         }
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      */
createBitmaps()88     public void createBitmaps() {
89         Bitmap.NativeWrapper dataA = Bitmap.allocNativeStorage(10, 10);
90         mFreeSignalA = dataA.mPhantomWrapper.mFreeSignal;
91         Bitmap.NativeWrapper dataB = Bitmap.allocNativeStorage(20, 20);
92         mFreeSignalB = dataB.mPhantomWrapper.mFreeSignal;
93 
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 }
99