Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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 package android.app.cts;
     17 
     18 import android.app.Activity;
     19 import android.content.Intent;
     20 import android.os.AsyncTask;
     21 import android.os.Bundle;
     22 import android.util.Log;
     23 
     24 /**
     25  * {@link Activity} that allocates arrays of 256k until reaching 75% of the specified memory class.
     26  */
     27 public class ActivityManagerMemoryClassTestActivity extends Activity {
     28 
     29     private static final String TAG = "ActivityManagerMemoryClassTest";
     30 
     31     private static final double FREE_MEMORY_PERCENTAGE = 0.75;
     32 
     33     private static final int ARRAY_BYTES_SIZE = 256 * 1024;
     34 
     35     @Override
     36     protected void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38         Intent intent = getIntent();
     39         int memoryClass =
     40                 intent.getIntExtra(ActivityManagerMemoryClassLaunchActivity.MEMORY_CLASS_EXTRA, -1);
     41         new AllocateMemoryTask(memoryClass).execute();
     42     }
     43 
     44     private class AllocateMemoryTask extends AsyncTask<Void, Void, Void> {
     45 
     46         private final int mMemoryClass;
     47 
     48         AllocateMemoryTask(int memoryClass) {
     49             this.mMemoryClass = memoryClass;
     50         }
     51 
     52         @Override
     53         protected Void doInBackground(Void... params) {
     54             int targetMbs = (int) (mMemoryClass * FREE_MEMORY_PERCENTAGE);
     55             int numArrays = targetMbs * 1024 * 1024 / ARRAY_BYTES_SIZE;
     56             Log.i(TAG, "Memory class: " + mMemoryClass + "mb Target memory: "
     57                     + targetMbs + "mb Number of arrays: " + numArrays);
     58 
     59             byte[][] arrays = new byte[numArrays][];
     60             for (int i = 0; i < arrays.length; i++) {
     61                 Log.i(TAG, "Allocating array " + i + " of " + arrays.length
     62                         + " (" + (i * ARRAY_BYTES_SIZE / 1024 / 1024) + "mb)");
     63                 arrays[i] = new byte[ARRAY_BYTES_SIZE];
     64             }
     65             return null;
     66         }
     67 
     68         @Override
     69         protected void onPostExecute(Void result) {
     70             super.onPostExecute(result);
     71             setResult(RESULT_OK);
     72             finish();
     73         }
     74     }
     75 }
     76