1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2009 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CountDownLatch; 18*795d594fSAndroid Build Coastguard Worker 19*795d594fSAndroid Build Coastguard Worker public class Main { 20*795d594fSAndroid Build Coastguard Worker Bitmap mBitmap1, mBitmap2, mBitmap3, mBitmap4; 21*795d594fSAndroid Build Coastguard Worker CountDownLatch mFreeSignalA, mFreeSignalB; 22*795d594fSAndroid Build Coastguard Worker sleep(int ms)23*795d594fSAndroid Build Coastguard Worker public static void sleep(int ms) { 24*795d594fSAndroid Build Coastguard Worker try { 25*795d594fSAndroid Build Coastguard Worker Thread.sleep(ms); 26*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException ie) { 27*795d594fSAndroid Build Coastguard Worker System.out.println("sleep interrupted"); 28*795d594fSAndroid Build Coastguard Worker } 29*795d594fSAndroid Build Coastguard Worker } 30*795d594fSAndroid Build Coastguard Worker main(String args[])31*795d594fSAndroid Build Coastguard Worker public static void main(String args[]) { 32*795d594fSAndroid Build Coastguard Worker System.out.println("start"); 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker Main main = new Main(); 35*795d594fSAndroid Build Coastguard Worker main.run(); 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker System.out.println("done"); 38*795d594fSAndroid Build Coastguard Worker } 39*795d594fSAndroid Build Coastguard Worker run()40*795d594fSAndroid Build Coastguard Worker public void run() { 41*795d594fSAndroid Build Coastguard Worker createBitmaps(); 42*795d594fSAndroid Build Coastguard Worker 43*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 44*795d594fSAndroid Build Coastguard Worker sleep(250); 45*795d594fSAndroid Build Coastguard Worker 46*795d594fSAndroid Build Coastguard Worker mBitmap2.drawAt(0, 0); 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker System.out.println("nulling 1"); 49*795d594fSAndroid Build Coastguard Worker mBitmap1 = null; 50*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 51*795d594fSAndroid Build Coastguard Worker try { 52*795d594fSAndroid Build Coastguard Worker mFreeSignalA.await(); // Block until dataA is definitely freed. 53*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException e) { 54*795d594fSAndroid Build Coastguard Worker System.out.println("got unexpected InterruptedException e: " + e); 55*795d594fSAndroid Build Coastguard Worker } 56*795d594fSAndroid Build Coastguard Worker 57*795d594fSAndroid Build Coastguard Worker System.out.println("nulling 2"); 58*795d594fSAndroid Build Coastguard Worker mBitmap2 = null; 59*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 60*795d594fSAndroid Build Coastguard Worker sleep(200); 61*795d594fSAndroid Build Coastguard Worker 62*795d594fSAndroid Build Coastguard Worker System.out.println("nulling 3"); 63*795d594fSAndroid Build Coastguard Worker mBitmap3 = null; 64*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 65*795d594fSAndroid Build Coastguard Worker sleep(200); 66*795d594fSAndroid Build Coastguard Worker 67*795d594fSAndroid Build Coastguard Worker System.out.println("nulling 4"); 68*795d594fSAndroid Build Coastguard Worker mBitmap4 = null; 69*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 70*795d594fSAndroid Build Coastguard Worker try { 71*795d594fSAndroid Build Coastguard Worker mFreeSignalB.await(); // Block until dataB is definitely freed. 72*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException e) { 73*795d594fSAndroid Build Coastguard Worker System.out.println("got unexpected InterruptedException e: " + e); 74*795d594fSAndroid Build Coastguard Worker } 75*795d594fSAndroid Build Coastguard Worker 76*795d594fSAndroid Build Coastguard Worker Bitmap.shutDown(); 77*795d594fSAndroid Build Coastguard Worker } 78*795d594fSAndroid Build Coastguard Worker 79*795d594fSAndroid Build Coastguard Worker /* 80*795d594fSAndroid Build Coastguard Worker * Create bitmaps. 81*795d594fSAndroid Build Coastguard Worker * 82*795d594fSAndroid Build Coastguard Worker * bitmap1 is 10x10 and unique 83*795d594fSAndroid Build Coastguard Worker * bitmap2 and bitmap3 are 20x20 and share the same storage. 84*795d594fSAndroid Build Coastguard Worker * bitmap4 is just another reference to bitmap3 85*795d594fSAndroid Build Coastguard Worker * 86*795d594fSAndroid Build Coastguard Worker * When we return there should be no local refs lurking on the stack. 87*795d594fSAndroid Build Coastguard Worker */ createBitmaps()88*795d594fSAndroid Build Coastguard Worker public void createBitmaps() { 89*795d594fSAndroid Build Coastguard Worker Bitmap.NativeWrapper dataA = Bitmap.allocNativeStorage(10, 10); 90*795d594fSAndroid Build Coastguard Worker mFreeSignalA = dataA.mPhantomWrapper.mFreeSignal; 91*795d594fSAndroid Build Coastguard Worker Bitmap.NativeWrapper dataB = Bitmap.allocNativeStorage(20, 20); 92*795d594fSAndroid Build Coastguard Worker mFreeSignalB = dataB.mPhantomWrapper.mFreeSignal; 93*795d594fSAndroid Build Coastguard Worker 94*795d594fSAndroid Build Coastguard Worker mBitmap1 = new Bitmap("one", 10, 10, dataA); 95*795d594fSAndroid Build Coastguard Worker mBitmap2 = new Bitmap("two", 20, 20, dataB); 96*795d594fSAndroid Build Coastguard Worker mBitmap3 = mBitmap4 = new Bitmap("three/four", 20, 20, dataB); 97*795d594fSAndroid Build Coastguard Worker } 98*795d594fSAndroid Build Coastguard Worker } 99