1 /* 2 * Copyright (C) 2008 Google Inc. 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 com.android.hit; 18 19 import java.util.ArrayList; 20 import java.util.HashSet; 21 import java.util.Set; 22 23 public abstract class Instance { 24 long mId; 25 26 // Id of the ClassObj of which this object is an instance 27 long mClassId; 28 29 // The stack in which this object was allocated 30 StackTrace mStack; 31 32 // The heap in which this object was allocated (app, zygote, etc) 33 Heap mHeap; 34 35 // The size of this object 36 int mSize; 37 38 public interface Filter { accept(Instance instance)39 public boolean accept(Instance instance); 40 } 41 42 // List of all objects that hold a live reference to this object 43 private ArrayList<Instance> mParents; 44 45 /* 46 * After the whole HPROF file is read and parsed this method will be 47 * called on all heap objects so that they can resolve their internal 48 * object references. 49 * 50 * The super-State is passed in because some object references (such 51 * as interned Strings and static class fields) may need to be searched 52 * for in a heap other than the one this Instance is in. 53 */ resolveReferences(State state)54 public abstract void resolveReferences(State state); 55 56 /* 57 * Some operations require gathering all the objects in a given section 58 * of the object graph. If non-null, the filter is applied to each 59 * node in the graph to determine if it should be added to the result 60 * set. 61 */ visit(Set<Instance> resultSet, Filter filter)62 public abstract void visit(Set<Instance> resultSet, Filter filter); 63 setSize(int size)64 public void setSize(int size) { 65 mSize = size; 66 } 67 getCompositeSize()68 public final int getCompositeSize() { 69 HashSet<Instance> set = new HashSet<Instance>(); 70 71 visit(set, null); 72 73 int size = 0; 74 75 for (Instance instance: set) { 76 size += instance.getSize(); 77 } 78 79 return size; 80 } 81 82 // Returns the instrinsic size of a given object getSize()83 public int getSize() { 84 return mSize; 85 } 86 getTypeName()87 public abstract String getTypeName(); 88 setHeap(Heap heap)89 public void setHeap(Heap heap) { 90 mHeap = heap; 91 } 92 93 // Add to the list of objects that have a hard reference to this Instance addParent(Instance parent)94 public void addParent(Instance parent) { 95 if (mParents == null) { 96 mParents = new ArrayList<Instance>(); 97 } 98 99 mParents.add(parent); 100 } 101 getParents()102 public ArrayList<Instance> getParents() { 103 if (mParents == null) { 104 mParents = new ArrayList<Instance>(); 105 } 106 107 return mParents; 108 } 109 110 /* 111 * If this object has a reference to the object identified by id, return 112 * a String describing the reference in detail. 113 */ describeReferenceTo(long id)114 public String describeReferenceTo(long id) { 115 return "No reference to 0x" + Long.toHexString(id); 116 } 117 } 118