Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.carrotsearch.sizeof;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.*;
import java.util.*;
import com.carrotsearch.sizeof.RamUsageEstimator;
/**
* Dumps retained and shallow memory taken by an object and its reference
* hierarchy. This could be improved in a number of ways but works for debugging
* and "live" object monitoring. For static heap analyses a real performance
* monitor like YourKit is much better (and accurate?).
*
* @see #dump(PrintWriter, Object)
* @see #dump(Object)
*/
public class ObjectTree {
/**
* Dump the object tree to a {@link PrintWriter}.
*/
public static void dump(PrintWriter pw, Object root) {
Node nodeTree = Node.create(root);
printTree(new StringBuilder(), new StringBuilder(), pw, nodeTree);
}
/**
* Dump the object tree to a {@link String}.
*/
public static String dump(Object root) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
dump(pw, root);
pw.flush();
return sw.toString();
}
private static class Node {
private String name;
private List children;
private long shallowSize;
private long deepSize;
public Node(String name, Object delegate) {
this.name = name;
if (delegate != null) {
shallowSize = RamUsageEstimator.shallowSizeOf(delegate);
deepSize = shallowSize;
}
}
private void addChild(Node node) {
if (children == null) {
children = new ArrayList();
}
children.add(node);
deepSize += node.deepSize;
}
/** Factory create method. */
public static Node create(Object delegate) {
return create("root", delegate, new IdentityHashMap