1 /* 2 * Copyright (C) 2017 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 package libcore.heapdumper; 18 19 import android.app.ActivityManager; 20 import android.os.Debug; 21 import android.os.Process; 22 import android.util.Log; 23 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.IOException; 27 import java.io.OutputStreamWriter; 28 import java.nio.charset.Charset; 29 import java.nio.charset.StandardCharsets; 30 31 /** 32 * A specialization of {@link AbstractMetricInstrumentation} where the measurement taken is the 33 * process's total PSS in kB. 34 */ 35 public class PssInstrumentation extends AbstractMetricInstrumentation { 36 37 private static final String TAG = "PssInstrumentation"; 38 39 @Override takeMeasurement(String label)40 protected void takeMeasurement(String label) throws IOException { 41 ActivityManager activityManager = getContext().getSystemService(ActivityManager.class); 42 tryRemoveGarbage(); 43 int totalPssKb = Math.toIntExact(Debug.getPss()); 44 File output = resolveRelativeOutputFilename(label + ".pss.txt"); 45 Charset cs = StandardCharsets.UTF_8; 46 try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(output), cs)) { 47 writer.append(Integer.toString(totalPssKb)); 48 } 49 Log.i(TAG, "Wrote to total PSS of " + totalPssKb + " kB to " + output.getCanonicalPath()); 50 } 51 } 52