本文整理汇总了Java中com.squareup.haha.perflib.Snapshot类的典型用法代码示例。如果您正苦于以下问题:Java Snapshot类的具体用法?Java Snapshot怎么用?Java Snapshot使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Snapshot类属于com.squareup.haha.perflib包,在下文中一共展示了Snapshot类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeIgnoredBitmapRetainedSize
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
/**
* Bitmaps and bitmap byte arrays are sometimes held by native gc roots, so they aren't included
* in the retained size because their root dominator is a native gc root.
* To fix this, we check if the leaking instance is a dominator for each bitmap instance and then
* add the bitmap size.
*
* From experience, we've found that bitmap created in code (Bitmap.createBitmap()) are correctly
* accounted for, however bitmaps set in layouts are not.
*/
private int computeIgnoredBitmapRetainedSize(Snapshot snapshot, Instance leakingInstance) {
int bitmapRetainedSize = 0;
ClassObj bitmapClass = snapshot.findClass("android.graphics.Bitmap");
for (Instance bitmapInstance : bitmapClass.getInstancesList()) {
if (isIgnoredDominator(leakingInstance, bitmapInstance)) {
ArrayInstance mBufferInstance = fieldValue(classInstanceValues(bitmapInstance), "mBuffer");
// Native bitmaps have mBuffer set to null. We sadly can't account for them.
if (mBufferInstance == null) {
continue;
}
long bufferSize = mBufferInstance.getTotalRetainedSize();
long bitmapSize = bitmapInstance.getTotalRetainedSize();
// Sometimes the size of the buffer isn't accounted for in the bitmap retained size. Since
// the buffer is large, it's easy to detect by checking for bitmap size < buffer size.
if (bitmapSize < bufferSize) {
bitmapSize += bufferSize;
}
bitmapRetainedSize += bitmapSize;
}
}
return bitmapRetainedSize;
}
示例2: ensureUniqueRoots
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
@Test
public void ensureUniqueRoots() {
Snapshot snapshot = createSnapshot(DUP_ROOTS);
heapAnalyzer.deduplicateGcRoots(snapshot);
Collection<RootObj> uniqueRoots = snapshot.getGCRoots();
assertThat(uniqueRoots).hasSize(4);
List<Long> rootIds = new ArrayList<>();
for (RootObj root : uniqueRoots) {
rootIds.add(root.getId());
}
Collections.sort(rootIds);
// 3 appears twice because even though two RootObjs have the same id, they're different types.
assertThat(rootIds).containsExactly(3L, 3L, 5L, 6L);
}
示例3: computeIgnoredBitmapRetainedSize
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
/**
* Bitmaps and bitmap byte arrays are sometimes held by native gc roots, so they aren't included
* in the retained size because their root dominator is a native gc root.
* To fix this, we check if the leaking instance is a dominator for each bitmap instance and then
* add the bitmap size.
*
* From experience, we've found that bitmap created in code (Bitmap.createBitmap()) are correctly
* accounted for, however bitmaps set in layouts are not.
*/
private long computeIgnoredBitmapRetainedSize(Snapshot snapshot, Instance leakingInstance) {
long bitmapRetainedSize = 0;
ClassObj bitmapClass = snapshot.findClass("android.graphics.Bitmap");
for (Instance bitmapInstance : bitmapClass.getInstancesList()) {
if (isIgnoredDominator(leakingInstance, bitmapInstance)) {
ArrayInstance mBufferInstance = fieldValue(classInstanceValues(bitmapInstance), "mBuffer");
// Native bitmaps have mBuffer set to null. We sadly can't account for them.
if (mBufferInstance == null) {
continue;
}
long bufferSize = mBufferInstance.getTotalRetainedSize();
long bitmapSize = bitmapInstance.getTotalRetainedSize();
// Sometimes the size of the buffer isn't accounted for in the bitmap retained size. Since
// the buffer is large, it's easy to detect by checking for bitmap size < buffer size.
if (bitmapSize < bufferSize) {
bitmapSize += bufferSize;
}
bitmapRetainedSize += bitmapSize;
}
}
return bitmapRetainedSize;
}
示例4: checkForLeak
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
/**
* Searches the heap dump for a {@link KeyedWeakReference} instance with the corresponding key,
* and then computes the shortest strong reference path from that instance to the GC roots.
*/
public AnalysisResult checkForLeak(File heapDumpFile, String referenceKey) {
long analysisStartNanoTime = System.nanoTime();
if (!heapDumpFile.exists()) {
Exception exception = new IllegalArgumentException("File does not exist: " + heapDumpFile);
return failure(exception, since(analysisStartNanoTime));
}
try {
HprofBuffer buffer = new MemoryMappedFileBuffer(heapDumpFile);
HprofParser parser = new HprofParser(buffer);
Snapshot snapshot = parser.parse();
deduplicateGcRoots(snapshot);
Instance leakingRef = findLeakingReference(referenceKey, snapshot);
// False alarm, weak reference was cleared in between key check and heap dump.
if (leakingRef == null) {
return noLeak(since(analysisStartNanoTime));
}
return findLeakTrace(analysisStartNanoTime, snapshot, leakingRef);
} catch (Throwable e) {
return failure(e, since(analysisStartNanoTime));
}
}
示例5: findLeakingReference
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private Instance findLeakingReference(String key, Snapshot snapshot) {
ClassObj refClass = snapshot.findClass(KeyedWeakReference.class.getName());
List<String> keysFound = new ArrayList<String>();
for (Instance instance : refClass.getInstancesList()) {
List<ClassInstance.FieldValue> values = classInstanceValues(instance);
String keyCandidate = asString(fieldValue(values, "key"));
if (keyCandidate.equals(key)) {
return fieldValue(values, "referent");
}
keysFound.add(keyCandidate);
}
throw new IllegalStateException(
"Could not find weak reference with key " + key + " in " + keysFound);
}
示例6: createSnapshot
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private Snapshot createSnapshot(List<RootObj> gcRoots) {
Snapshot snapshot = new Snapshot(null);
for (RootObj root : gcRoots) {
snapshot.addRoot(root);
}
return snapshot;
}
示例7: setUp
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
@Before
public void setUp() {
buffer = new FakeHprofBuffer();
snapshot = new Snapshot(buffer);
// set HPROF identifier size; required for Object instance field lookups
// cf. https://java.net/downloads/heap-snapshot/hprof-binary-format.html
snapshot.setIdSize(4);
}
示例8: addStringClassToSnapshotWithFields
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private void addStringClassToSnapshotWithFields(Snapshot snapshot, Field[] fields) {
ClassObj charArrayClass = new ClassObj(0, null, "char[]", 0);
snapshot.addClass(CHAR_ARRAY_CLASS_ID, charArrayClass);
ClassObj stringClass = new ClassObj(0, null, "string", 0);
stringClass.setFields(fields);
snapshot.addClass(STRING_CLASS_ID, stringClass);
}
示例9: findTrackedReferences
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
public List<TrackedReference> findTrackedReferences(File heapDumpFile) {
if (!heapDumpFile.exists()) {
throw new IllegalArgumentException("File does not exist: " + heapDumpFile);
}
try {
HprofBuffer buffer = new MemoryMappedFileBuffer(heapDumpFile);
HprofParser parser = new HprofParser(buffer);
Snapshot snapshot = parser.parse();
deduplicateGcRoots(snapshot);
ClassObj refClass = snapshot.findClass(KeyedWeakReference.class.getName());
List<TrackedReference> references = new ArrayList<>();
for (Instance weakRef : refClass.getInstancesList()) {
List<ClassInstance.FieldValue> values = classInstanceValues(weakRef);
String key = asString(fieldValue(values, "key"));
String name =
hasField(values, "name") ? asString(fieldValue(values, "name")) : "(No name field)";
Instance instance = fieldValue(values, "referent");
if (instance != null) {
String className = getClassName(instance);
List<String> fields = describeFields(instance);
references.add(new TrackedReference(key, name, className, fields));
}
}
return references;
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
示例10: findLeakingReference
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private Instance findLeakingReference(String key, Snapshot snapshot) {
ClassObj refClass = snapshot.findClass(KeyedWeakReference.class.getName());
List<String> keysFound = new ArrayList<>();
for (Instance instance : refClass.getInstancesList()) {
List<ClassInstance.FieldValue> values = classInstanceValues(instance);
String keyCandidate = asString(fieldValue(values, "key"));
if (keyCandidate.equals(key)) {
return fieldValue(values, "referent");
}
keysFound.add(keyCandidate);
}
throw new IllegalStateException(
"Could not find weak reference with key " + key + " in " + keysFound);
}
示例11: findLeakTrace
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private AnalysisResult findLeakTrace(long analysisStartNanoTime, Snapshot snapshot,
Instance leakingRef) {
ShortestPathFinder pathFinder = new ShortestPathFinder(excludedRefs);
ShortestPathFinder.Result result = pathFinder.findPath(snapshot, leakingRef);
// False alarm, no strong reference path to GC Roots.
if (result.leakingNode == null) {
return noLeak(since(analysisStartNanoTime));
}
LeakTrace leakTrace = buildLeakTrace(result.leakingNode);
String className = leakingRef.getClassObj().getClassName();
// Side effect: computes retained size.
snapshot.computeDominators();
Instance leakingInstance = result.leakingNode.instance;
long retainedSize = leakingInstance.getTotalRetainedSize();
// TODO: check O sources and see what happened to android.graphics.Bitmap.mBuffer
if (SDK_INT <= N_MR1) {
retainedSize += computeIgnoredBitmapRetainedSize(snapshot, leakingInstance);
}
return leakDetected(result.excludingKnownLeaks, className, leakTrace, retainedSize,
since(analysisStartNanoTime));
}
示例12: addStringClassToSnapshotWithFields_O
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private void addStringClassToSnapshotWithFields_O(Snapshot snapshot, Field[] fields) {
ClassObj byteArrayClass = new ClassObj(0, null, "byte[]", 0);
snapshot.addClass(BYTE_ARRAY_CLASS_ID, byteArrayClass);
ClassObj stringClass = new ClassObj(0, null, "string", 0);
stringClass.setFields(fields);
snapshot.addClass(STRING_CLASS_ID, stringClass);
}
示例13: findPath
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
Result findPath(Snapshot snapshot, Instance leakingRef) {
clearState();
canIgnoreStrings = !isString(leakingRef);
enqueueGcRoots(snapshot);
boolean excludingKnownLeaks = false;
LeakNode leakingNode = null;
while (!toVisitQueue.isEmpty() || !toVisitIfNoPathQueue.isEmpty()) {
LeakNode node;
if (!toVisitQueue.isEmpty()) {
node = toVisitQueue.poll();
} else {
node = toVisitIfNoPathQueue.poll();
if (node.exclusion == null) {
throw new IllegalStateException("Expected node to have an exclusion " + node);
}
excludingKnownLeaks = true;
}
// Termination
if (node.instance == leakingRef) {
leakingNode = node;
break;
}
if (checkSeen(node)) {
continue;
}
if (node.instance instanceof RootObj) {
visitRootObj(node);
} else if (node.instance instanceof ClassObj) {
visitClassObj(node);
} else if (node.instance instanceof ClassInstance) {
visitClassInstance(node);
} else if (node.instance instanceof ArrayInstance) {
visitArrayInstance(node);
} else {
throw new IllegalStateException("Unexpected type for " + node.instance);
}
}
return new Result(leakingNode, excludingKnownLeaks);
}
示例14: enqueueGcRoots
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private void enqueueGcRoots(Snapshot snapshot) {
for (RootObj rootObj : snapshot.getGCRoots()) {
switch (rootObj.getRootType()) {
case JAVA_LOCAL:
Instance thread = HahaSpy.allocatingThread(rootObj);
String threadName = threadName(thread);
Exclusion params = excludedRefs.threadNames.get(threadName);
if (params == null || !params.alwaysExclude) {
enqueue(params, null, rootObj, null, null);
}
break;
case INTERNED_STRING:
case DEBUGGER:
case INVALID_TYPE:
// An object that is unreachable from any other root, but not a root itself.
case UNREACHABLE:
case UNKNOWN:
// An object that is in a queue, waiting for a finalizer to run.
case FINALIZING:
break;
case SYSTEM_CLASS:
case VM_INTERNAL:
// A local variable in native code.
case NATIVE_LOCAL:
// A global variable in native code.
case NATIVE_STATIC:
// An object that was referenced from an active thread block.
case THREAD_BLOCK:
// Everything that called the wait() or notify() methods, or that is synchronized.
case BUSY_MONITOR:
case NATIVE_MONITOR:
case REFERENCE_CLEANUP:
// Input or output parameters in native code.
case NATIVE_STACK:
case JAVA_STATIC:
enqueue(null, null, rootObj, null, null);
break;
default:
throw new UnsupportedOperationException("Unknown root type:" + rootObj.getRootType());
}
}
}
示例15: initSnapshot
import com.squareup.haha.perflib.Snapshot; //导入依赖的package包/类
private void initSnapshot(HprofBuffer buffer) {
snapshot = new Snapshot(buffer);
// set HPROF identifier size; required for Object instance field lookups
// cf. https://java.net/downloads/heap-snapshot/hprof-binary-format.html
snapshot.setIdSize(4);
}