2001-09-20 00:02:59 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1999-10-07 02:11:44 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
1999-10-07 02:11:44 +00:00
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
2001-09-20 00:02:59 +00:00
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-10-07 02:11:44 +00:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
2002-03-19 04:30:17 +00:00
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
1999-10-07 02:11:44 +00:00
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
1999-10-07 02:11:44 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-10-07 02:11:44 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* Patrick C. Beard <beard@netscape.com>
|
2001-09-19 22:39:41 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* terms of the GNU Public License (the "GPL"), in which case the
|
|
|
|
* provisions of the GPL are applicable instead of those above.
|
|
|
|
* If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of the GPL and not to allow others to use your
|
|
|
|
* version of this file under the NPL, indicate your decision by
|
|
|
|
* deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this
|
|
|
|
* file under either the NPL or the GPL.
|
|
|
|
*/
|
1999-10-07 02:11:44 +00:00
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
class Leak extends Reference {
|
|
|
|
String mName;
|
2000-04-10 17:42:24 +00:00
|
|
|
long mCrawlOffset;
|
2000-09-22 07:32:52 +00:00
|
|
|
short mCrawlCount;
|
|
|
|
short mRefCount;
|
|
|
|
short mChildCount;
|
1999-10-21 23:58:28 +00:00
|
|
|
Leak[] mParents;
|
1999-10-29 06:04:01 +00:00
|
|
|
int mTotalSize;
|
2000-09-22 07:32:52 +00:00
|
|
|
boolean mMarked;
|
1999-10-07 02:11:44 +00:00
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
Leak(String addr, Type type, Object[] refs, long crawlOffset, short crawlCount) {
|
|
|
|
super(addr, type, refs);
|
|
|
|
mName = addr;
|
2000-04-10 17:42:24 +00:00
|
|
|
mCrawlOffset = crawlOffset;
|
|
|
|
mCrawlCount = crawlCount;
|
1999-10-07 02:11:44 +00:00
|
|
|
mRefCount = 0;
|
2000-09-22 07:32:52 +00:00
|
|
|
mChildCount = 0;
|
|
|
|
mParents = null;
|
1999-10-29 06:04:01 +00:00
|
|
|
mTotalSize = 0;
|
2000-09-22 07:32:52 +00:00
|
|
|
mMarked = false;
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|
1999-10-21 23:58:28 +00:00
|
|
|
|
|
|
|
void setParents(Vector parents) {
|
|
|
|
mParents = new Leak[parents.size()];
|
|
|
|
parents.copyInto(mParents);
|
|
|
|
}
|
1999-10-29 06:04:01 +00:00
|
|
|
|
|
|
|
void computeTotalSize() {
|
|
|
|
// first, mark this node as having been visited.
|
|
|
|
// we only want to include nodes that haven't been
|
|
|
|
// visited in our total size.
|
|
|
|
mTotalSize = mType.mSize;
|
|
|
|
|
|
|
|
// then, visit all nodes that haven't been visited,
|
|
|
|
// and include their total size in ours.
|
|
|
|
int count = mReferences.length;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
Object ref = mReferences[i];
|
|
|
|
if (ref instanceof Leak) {
|
|
|
|
Leak leak = (Leak) ref;
|
|
|
|
if (leak.mTotalSize == 0) {
|
|
|
|
leak.computeTotalSize();
|
|
|
|
mTotalSize += leak.mTotalSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearTotalSize() {
|
|
|
|
// first, clear our total size.
|
|
|
|
mTotalSize = 0;
|
|
|
|
|
|
|
|
// then, visit all nodes that haven't been visited,
|
|
|
|
// and clear each one's total size.
|
|
|
|
int count = mReferences.length;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
Object ref = mReferences[i];
|
|
|
|
if (ref instanceof Leak) {
|
|
|
|
Leak leak = (Leak) ref;
|
|
|
|
if (leak.mTotalSize != 0)
|
|
|
|
leak.clearTotalSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-10-07 02:11:44 +00:00
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
void clearMarks() {
|
|
|
|
// first, clear mark.
|
|
|
|
mMarked = false;
|
|
|
|
|
|
|
|
// then, visit all nodes that haven't been visited,
|
|
|
|
// and clear each one's mark.
|
|
|
|
int count = mReferences.length;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
Object ref = mReferences[i];
|
|
|
|
if (ref instanceof Leak) {
|
|
|
|
Leak leak = (Leak) ref;
|
|
|
|
if (leak.mMarked)
|
|
|
|
leak.clearMarks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static final char INDENT = '\t';
|
|
|
|
|
|
|
|
void printGraph(PrintWriter out) {
|
|
|
|
printGraph(out, 0);
|
|
|
|
clearMarks();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void printGraph(PrintWriter out, int indent) {
|
|
|
|
// first, mark this node as having been visited.
|
|
|
|
// we only want to include nodes that haven't been
|
|
|
|
// visited in our total size.
|
|
|
|
mMarked = true;
|
|
|
|
for (int i = 0; i < indent; ++i)
|
|
|
|
out.print(INDENT);
|
|
|
|
out.println(toString());
|
|
|
|
|
|
|
|
// then, visit all nodes that haven't been visited,
|
|
|
|
// and include their total size in ours.
|
|
|
|
int count = mReferences.length;
|
|
|
|
if (count > 0) {
|
|
|
|
int subIndent = indent + 1;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
Object ref = mReferences[i];
|
|
|
|
if (ref instanceof Leak) {
|
|
|
|
Leak leak = (Leak) ref;
|
|
|
|
if (!leak.mMarked)
|
|
|
|
leak.printGraph(out, subIndent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printCycle(PrintWriter out) {
|
|
|
|
printCycle(out, 0);
|
|
|
|
clearMarks();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void printCycle(PrintWriter out, int indent) {
|
|
|
|
// first, mark this node as having been visited.
|
|
|
|
// we only want to include nodes that haven't been
|
|
|
|
// visited in our total size.
|
|
|
|
mMarked = true;
|
|
|
|
|
|
|
|
// then, visit all nodes that haven't been visited,
|
|
|
|
// and include their total size in ours.
|
|
|
|
if (mChildCount > 0) {
|
|
|
|
// don't print leaf nodes in a cycle. they aren't interesting.
|
|
|
|
for (int i = 0; i < indent; ++i)
|
|
|
|
out.print(INDENT);
|
|
|
|
out.println(toString());
|
|
|
|
int subIndent = indent + 1;
|
|
|
|
int count = mReferences.length;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
Object ref = mReferences[i];
|
|
|
|
if (ref instanceof Leak) {
|
|
|
|
Leak leak = (Leak) ref;
|
|
|
|
if (!leak.mMarked)
|
|
|
|
leak.printCycle(out, subIndent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-07 02:11:44 +00:00
|
|
|
public String toString() {
|
2000-09-22 07:32:52 +00:00
|
|
|
return ("<A HREF=\"#" + mName + "\">" + mName + "</A> [" + mRefCount + "] " + mType + "{" + mTotalSize + "}");
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
/**
|
|
|
|
* Sorts in order of increasing reference count.
|
|
|
|
*/
|
|
|
|
static class ByRefCount extends QuickSort.Comparator {
|
1999-10-07 02:11:44 +00:00
|
|
|
public int compare(Object obj1, Object obj2) {
|
|
|
|
Leak l1 = (Leak) obj1, l2 = (Leak) obj2;
|
1999-10-29 06:04:01 +00:00
|
|
|
return (l1.mRefCount - l2.mRefCount);
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
1999-10-15 15:56:05 +00:00
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
/**
|
|
|
|
* Sorts in order of decreasing number of children.
|
|
|
|
*/
|
|
|
|
public static class ByChildCount extends QuickSort.Comparator {
|
|
|
|
public int compare(Object obj1, Object obj2) {
|
|
|
|
Leak l1 = (Leak) obj1, l2 = (Leak) obj2;
|
|
|
|
return (l2.mChildCount - l1.mChildCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-15 15:56:05 +00:00
|
|
|
/**
|
1999-10-29 06:04:01 +00:00
|
|
|
* Sorts in order of decreasing total size.
|
1999-10-15 15:56:05 +00:00
|
|
|
*/
|
2000-09-22 07:32:52 +00:00
|
|
|
static class ByTotalSize extends QuickSort.Comparator {
|
1999-10-15 15:56:05 +00:00
|
|
|
public int compare(Object obj1, Object obj2) {
|
|
|
|
Leak l1 = (Leak) obj1, l2 = (Leak) obj2;
|
1999-10-29 06:04:01 +00:00
|
|
|
return (l2.mTotalSize - l1.mTotalSize);
|
1999-10-15 15:56:05 +00:00
|
|
|
}
|
1999-10-08 04:12:11 +00:00
|
|
|
}
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
2000-04-10 17:42:24 +00:00
|
|
|
final class LineReader {
|
|
|
|
BufferedReader reader;
|
|
|
|
long offset;
|
|
|
|
|
|
|
|
LineReader(BufferedReader reader) {
|
|
|
|
this.reader = reader;
|
|
|
|
this.offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String readLine() throws IOException {
|
|
|
|
String line = reader.readLine();
|
|
|
|
if (line != null)
|
|
|
|
offset += 1 + line.length();
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() throws IOException {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-07 02:11:44 +00:00
|
|
|
public class leaksoup {
|
1999-12-10 20:23:16 +00:00
|
|
|
private static boolean ROOTS_ONLY = false;
|
|
|
|
|
1999-10-07 02:11:44 +00:00
|
|
|
public static void main(String[] args) {
|
1999-10-08 04:12:11 +00:00
|
|
|
if (args.length == 0) {
|
2000-04-10 17:42:24 +00:00
|
|
|
System.out.println("usage: leaksoup [-blame] [-lxr] [-assign] [-roots] leaks");
|
1999-10-08 04:12:11 +00:00
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
2000-04-10 17:42:24 +00:00
|
|
|
// assume user want's blame URLs.
|
|
|
|
FileLocator.USE_BLAME = true;
|
1999-11-06 03:24:08 +00:00
|
|
|
FileLocator.ASSIGN_BLAME = false;
|
1999-12-10 20:23:16 +00:00
|
|
|
ROOTS_ONLY = false;
|
1999-11-04 06:07:35 +00:00
|
|
|
|
1999-10-13 23:42:49 +00:00
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
String arg = args[i];
|
|
|
|
if (arg.charAt(0) == '-') {
|
|
|
|
if (arg.equals("-blame"))
|
1999-11-04 00:24:26 +00:00
|
|
|
FileLocator.USE_BLAME = true;
|
2000-04-10 17:42:24 +00:00
|
|
|
else if (arg.equals("-lxr"))
|
|
|
|
FileLocator.USE_BLAME = false;
|
1999-12-10 20:23:16 +00:00
|
|
|
else if (arg.equals("-assign"))
|
1999-11-06 03:24:08 +00:00
|
|
|
FileLocator.ASSIGN_BLAME = true;
|
1999-12-10 20:23:16 +00:00
|
|
|
else if (arg.equals("-roots"))
|
|
|
|
ROOTS_ONLY = true;
|
|
|
|
else
|
|
|
|
System.out.println("unrecognized option: " + arg);
|
1999-10-13 23:42:49 +00:00
|
|
|
} else {
|
1999-11-04 00:24:26 +00:00
|
|
|
cook(arg);
|
1999-10-13 23:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
1999-11-04 06:07:35 +00:00
|
|
|
|
|
|
|
// quit the application.
|
|
|
|
System.exit(0);
|
1999-11-04 00:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cook(String inputName) {
|
1999-10-07 02:11:44 +00:00
|
|
|
try {
|
|
|
|
Vector vec = new Vector();
|
1999-10-21 23:58:28 +00:00
|
|
|
Hashtable leakTable = new Hashtable();
|
1999-10-15 15:56:05 +00:00
|
|
|
Hashtable types = new Hashtable();
|
1999-10-08 04:12:11 +00:00
|
|
|
Histogram hist = new Histogram();
|
2000-04-10 17:42:24 +00:00
|
|
|
LineReader reader = new LineReader(new BufferedReader(new InputStreamReader(new FileInputStream(inputName))));
|
2000-04-13 06:35:02 +00:00
|
|
|
StringTable strings = new StringTable();
|
1999-10-07 02:11:44 +00:00
|
|
|
String line = reader.readLine();
|
|
|
|
while (line != null) {
|
|
|
|
if (line.startsWith("0x")) {
|
2000-04-13 06:35:02 +00:00
|
|
|
String addr = strings.intern(line.substring(0, 10));
|
|
|
|
String name = strings.intern(line.substring(line.indexOf('<') + 1, line.indexOf('>')));
|
1999-10-07 02:11:44 +00:00
|
|
|
int size;
|
|
|
|
try {
|
1999-10-08 04:12:11 +00:00
|
|
|
String str = line.substring(line.indexOf('(') + 1, line.indexOf(')')).trim();
|
1999-10-07 02:11:44 +00:00
|
|
|
size = Integer.parseInt(str);
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
size = 0;
|
|
|
|
}
|
1999-10-15 15:56:05 +00:00
|
|
|
|
|
|
|
// generate a unique type for this object.
|
2000-04-13 06:35:02 +00:00
|
|
|
String key = strings.intern(name + "_" + size);
|
1999-10-15 15:56:05 +00:00
|
|
|
Type type = (Type) types.get(key);
|
|
|
|
if (type == null) {
|
|
|
|
type = new Type(name, size);
|
|
|
|
types.put(key, type);
|
|
|
|
}
|
1999-10-12 18:59:52 +00:00
|
|
|
|
2000-04-10 17:42:24 +00:00
|
|
|
// read in fields. could compress these by converting to Integer objects.
|
1999-10-07 02:11:44 +00:00
|
|
|
vec.setSize(0);
|
|
|
|
for (line = reader.readLine(); line != null && line.charAt(0) == '\t'; line = reader.readLine())
|
2000-04-13 06:35:02 +00:00
|
|
|
vec.addElement(strings.intern(line.substring(1, 11)));
|
1999-10-07 02:11:44 +00:00
|
|
|
Object[] refs = new Object[vec.size()];
|
|
|
|
vec.copyInto(refs);
|
1999-10-12 18:59:52 +00:00
|
|
|
vec.setSize(0);
|
|
|
|
|
2000-04-10 17:42:24 +00:00
|
|
|
// record the offset of the stack crawl, which will be read in and formatted at the end, to save memory.
|
|
|
|
long crawlOffset = reader.offset;
|
2000-09-22 07:32:52 +00:00
|
|
|
short crawlCount = 0;
|
2000-04-10 17:42:24 +00:00
|
|
|
for (line = reader.readLine(); line != null && !line.startsWith("Leaked "); line = reader.readLine())
|
|
|
|
++crawlCount;
|
|
|
|
|
1999-10-12 18:59:52 +00:00
|
|
|
// record the leak.
|
2000-04-10 17:42:24 +00:00
|
|
|
leakTable.put(addr, new Leak(addr, type, refs, crawlOffset, crawlCount));
|
1999-10-13 23:42:49 +00:00
|
|
|
|
|
|
|
// count the leak types in a histogram.
|
1999-10-08 04:12:11 +00:00
|
|
|
hist.record(type);
|
1999-10-07 02:11:44 +00:00
|
|
|
} else {
|
|
|
|
line = reader.readLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
|
2000-04-13 06:35:02 +00:00
|
|
|
// don't need the interned strings table anymore.
|
|
|
|
strings = null;
|
|
|
|
|
1999-10-21 23:58:28 +00:00
|
|
|
Leak[] leaks = new Leak[leakTable.size()];
|
1999-10-07 02:11:44 +00:00
|
|
|
int leakCount = 0;
|
|
|
|
long totalSize = 0;
|
|
|
|
|
1999-10-21 23:58:28 +00:00
|
|
|
Hashtable parentTable = new Hashtable();
|
|
|
|
|
1999-10-07 02:11:44 +00:00
|
|
|
// now, we have a table full of leaked objects, lets derive reference counts, and build the graph.
|
1999-10-21 23:58:28 +00:00
|
|
|
Enumeration e = leakTable.elements();
|
1999-10-07 02:11:44 +00:00
|
|
|
while (e.hasMoreElements()) {
|
|
|
|
Leak leak = (Leak) e.nextElement();
|
|
|
|
Object[] refs = leak.mReferences;
|
|
|
|
int count = refs.length;
|
2000-09-22 07:32:52 +00:00
|
|
|
for (int r = 0; r < count; ++r) {
|
|
|
|
String addr = (String) refs[r];
|
1999-10-21 23:58:28 +00:00
|
|
|
Leak ref = (Leak) leakTable.get(addr);
|
1999-10-07 02:11:44 +00:00
|
|
|
if (ref != null) {
|
|
|
|
// increase the ref count.
|
|
|
|
ref.mRefCount++;
|
|
|
|
// change string to ref itself.
|
2000-09-22 07:32:52 +00:00
|
|
|
refs[r] = ref;
|
1999-10-21 23:58:28 +00:00
|
|
|
// add leak to ref's parents vector.
|
|
|
|
Vector parents = (Vector) parentTable.get(ref);
|
|
|
|
if (parents == null) {
|
|
|
|
parents = new Vector();
|
|
|
|
parentTable.put(ref, parents);
|
|
|
|
}
|
|
|
|
parents.addElement(leak);
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
leaks[leakCount++] = leak;
|
1999-10-13 23:42:49 +00:00
|
|
|
totalSize += leak.mType.mSize;
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|
1999-10-21 23:58:28 +00:00
|
|
|
|
|
|
|
// be nice to the GC.
|
|
|
|
leakTable.clear();
|
|
|
|
leakTable = null;
|
2000-09-22 07:32:52 +00:00
|
|
|
|
|
|
|
// sort the leaks by address, and find interior pointers.
|
|
|
|
{
|
|
|
|
QuickSort byAddress = new QuickSort(new Reference.ByAddress());
|
|
|
|
byAddress.sort(leaks);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < leakCount; ++i) {
|
|
|
|
Leak leak = leaks[i];
|
|
|
|
Object[] refs = leak.mReferences;
|
|
|
|
int count = refs.length;
|
|
|
|
short childCount = 0;
|
|
|
|
for (int r = 0; r < count; ++r) {
|
|
|
|
if (refs[r] instanceof String) {
|
|
|
|
String addr = (String) refs[r];
|
|
|
|
if (addr.equals("0x00000000")) continue;
|
|
|
|
int address = (int) Long.parseLong(addr.substring(2), 16);
|
|
|
|
Leak ref = (Leak) Reference.findNearest(leaks, address);
|
|
|
|
if (ref != null) {
|
|
|
|
// increase the ref count.
|
|
|
|
ref.mRefCount++;
|
|
|
|
// change string to ref itself.
|
|
|
|
refs[r] = ref;
|
|
|
|
// add leak to ref's parents vector.
|
|
|
|
Vector parents = (Vector) parentTable.get(ref);
|
|
|
|
if (parents == null) {
|
|
|
|
parents = new Vector();
|
|
|
|
parentTable.put(ref, parents);
|
|
|
|
}
|
|
|
|
parents.addElement(leak);
|
|
|
|
++childCount;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++childCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
leak.mChildCount = childCount;
|
|
|
|
}
|
1999-10-21 23:58:28 +00:00
|
|
|
|
|
|
|
// set the parents of each leak.
|
|
|
|
e = parentTable.keys();
|
|
|
|
while (e.hasMoreElements()) {
|
|
|
|
Leak leak = (Leak) e.nextElement();
|
|
|
|
Vector parents = (Vector) parentTable.get(leak);
|
|
|
|
if (parents != null)
|
|
|
|
leak.setParents(parents);
|
|
|
|
}
|
1999-10-07 02:11:44 +00:00
|
|
|
|
|
|
|
// be nice to the GC.
|
1999-10-21 23:58:28 +00:00
|
|
|
parentTable.clear();
|
|
|
|
parentTable = null;
|
1999-10-07 02:11:44 +00:00
|
|
|
|
1999-10-13 23:42:49 +00:00
|
|
|
// store the leak report in inputName + ".html"
|
1999-10-21 23:58:28 +00:00
|
|
|
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(inputName + ".html"))));
|
1999-10-15 15:56:05 +00:00
|
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
out.println("<TITLE>Leaks as of " + now + "</TITLE>");
|
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
// print leak summary.
|
|
|
|
out.println("<H2>Leak Summary</H2>");
|
|
|
|
out.println("total objects leaked = " + leakCount + "<BR>");
|
|
|
|
out.println("total memory leaked = " + totalSize + " bytes.<BR>");
|
|
|
|
|
|
|
|
printLeakHistogram(out, hist);
|
|
|
|
printLeakStructure(out, leaks);
|
|
|
|
|
2000-04-10 17:42:24 +00:00
|
|
|
// open original file again, as a RandomAccessFile, to read in stack crawl information.
|
|
|
|
|
1999-10-08 04:12:11 +00:00
|
|
|
// print the leak report.
|
2000-09-22 07:32:52 +00:00
|
|
|
if (!ROOTS_ONLY) {
|
|
|
|
RandomAccessFile in = new RandomAccessFile(inputName, "r");
|
2000-04-10 17:42:24 +00:00
|
|
|
printLeaks(in, out, leaks);
|
2000-09-22 07:32:52 +00:00
|
|
|
in.close();
|
|
|
|
}
|
1999-10-08 04:12:11 +00:00
|
|
|
|
|
|
|
out.close();
|
1999-10-07 02:11:44 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace(System.err);
|
|
|
|
}
|
|
|
|
}
|
1999-10-08 04:12:11 +00:00
|
|
|
|
1999-10-15 15:56:05 +00:00
|
|
|
/**
|
|
|
|
* Sorts the bins of a histogram by (count * typeSize) to show the
|
|
|
|
* most pressing leaks.
|
|
|
|
*/
|
2000-09-22 07:32:52 +00:00
|
|
|
static class ByTypeBinSize extends QuickSort.Comparator {
|
1999-10-15 15:56:05 +00:00
|
|
|
Histogram hist;
|
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
ByTypeBinSize(Histogram hist) {
|
1999-10-15 15:56:05 +00:00
|
|
|
this.hist = hist;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compare(Object obj1, Object obj2) {
|
|
|
|
Type t1 = (Type) obj1, t2 = (Type) obj2;
|
1999-10-19 03:03:12 +00:00
|
|
|
return (hist.count(t1) * t1.mSize - hist.count(t2) * t2.mSize);
|
1999-10-15 15:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
static void printLeakHistogram(PrintWriter out, Histogram hist) throws IOException {
|
2000-09-02 03:29:30 +00:00
|
|
|
// sort the types by histogram count.
|
|
|
|
Object[] types = hist.objects();
|
2000-09-22 07:32:52 +00:00
|
|
|
QuickSort byTypeBinSize = new QuickSort(new ByTypeBinSize(hist));
|
|
|
|
byTypeBinSize.sort(types);
|
1999-10-15 15:56:05 +00:00
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
out.println("<H2>Leak Histogram</H2>");
|
1999-10-15 15:56:05 +00:00
|
|
|
out.println("<PRE>");
|
2000-09-02 03:29:30 +00:00
|
|
|
int index = types.length;
|
|
|
|
while (index > 0) {
|
|
|
|
Type type = (Type) types[--index];
|
|
|
|
int count = hist.count(type);
|
|
|
|
out.println(type.toString() + " : " + count + " {" + (count * type.mSize) + "}");
|
1999-10-15 15:56:05 +00:00
|
|
|
}
|
|
|
|
out.println("</PRE>");
|
|
|
|
}
|
1999-12-10 20:23:16 +00:00
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
static void printLeakStructure(PrintWriter out, Leak[] leaks) {
|
|
|
|
// print root leaks. consider only leaks with a reference
|
|
|
|
// count of 0, which when fixed, will hopefully reclaim
|
|
|
|
// all of the objects below them in the graph.
|
|
|
|
{
|
|
|
|
QuickSort byRefCount = new QuickSort(new Leak.ByRefCount());
|
|
|
|
byRefCount.sort(leaks);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rootCount = 0;
|
|
|
|
int leakCount = leaks.length;
|
|
|
|
for (int i = 0; i < leakCount; ++i) {
|
|
|
|
Leak leak = leaks[i];
|
|
|
|
if (leak.mRefCount > 0)
|
|
|
|
break;
|
|
|
|
++rootCount;
|
|
|
|
leak.computeTotalSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QuickSort byTotalSize = new QuickSort(new Leak.ByTotalSize());
|
|
|
|
byTotalSize.sort(leaks, rootCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.println("<H2>Leak Roots</H2>");
|
|
|
|
out.println("<PRE>");
|
|
|
|
for (int i = 0; i < rootCount; ++i) {
|
|
|
|
Leak leak = leaks[i];
|
|
|
|
leak.printGraph(out);
|
|
|
|
}
|
|
|
|
out.println("</PRE>");
|
|
|
|
|
|
|
|
// print leak cycles. traverse the leaks from objects with most number
|
|
|
|
// of children to least, so that leaf objects will be printed after
|
|
|
|
// their parents.
|
|
|
|
{
|
|
|
|
QuickSort byChildCount = new QuickSort(new Leak.ByChildCount());
|
|
|
|
byChildCount.sort(leaks);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.println("<H2>Leak Cycles</H2>");
|
|
|
|
out.println("<PRE>");
|
|
|
|
for (int i = 0; i < leakCount; ++i) {
|
|
|
|
Leak leak = leaks[i];
|
|
|
|
// if an object's total size isn't known yet, then it must
|
|
|
|
// be a member of a cycle, since it wasn't reached when traversing roots.
|
|
|
|
if (leak.mTotalSize == 0) {
|
|
|
|
leak.computeTotalSize();
|
|
|
|
leak.printCycle(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.println("</PRE>");
|
|
|
|
}
|
|
|
|
|
1999-12-10 20:23:16 +00:00
|
|
|
static StringBuffer appendChar(StringBuffer buffer, int ch) {
|
|
|
|
if (ch > 32 && ch < 0x7F) {
|
|
|
|
switch (ch) {
|
|
|
|
case '<': buffer.append("<"); break;
|
|
|
|
case '>': buffer.append(">"); break;
|
|
|
|
default: buffer.append((char)ch); break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer.append("·");
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printField(PrintWriter out, Object field) {
|
|
|
|
String value = field.toString();
|
|
|
|
if (field instanceof String) {
|
|
|
|
// this is just a plain HEX value, print its contents as ASCII as well.
|
|
|
|
if (value.startsWith("0x")) {
|
|
|
|
try {
|
|
|
|
int hexValue = Integer.parseInt(value.substring(2), 16);
|
2000-01-30 19:04:41 +00:00
|
|
|
// don't interpret some common values, to save some space.
|
|
|
|
if (hexValue != 0 && hexValue != -1) {
|
|
|
|
StringBuffer buffer = new StringBuffer(value);
|
|
|
|
buffer.append('\t');
|
|
|
|
appendChar(buffer, ((hexValue >>> 24) & 0x00FF));
|
|
|
|
appendChar(buffer, ((hexValue >>> 16) & 0x00FF));
|
|
|
|
appendChar(buffer, ((hexValue >>> 8) & 0x00FF));
|
|
|
|
appendChar(buffer, (hexValue & 0x00FF));
|
|
|
|
value = buffer.toString();
|
|
|
|
}
|
1999-12-10 20:23:16 +00:00
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.println("\t" + value);
|
|
|
|
}
|
1999-10-15 15:56:05 +00:00
|
|
|
|
2000-04-10 17:42:24 +00:00
|
|
|
static void printLeaks(RandomAccessFile in, PrintWriter out, Leak[] leaks) throws IOException {
|
1999-12-10 20:23:16 +00:00
|
|
|
// sort the leaks by total size.
|
1999-10-29 06:04:01 +00:00
|
|
|
QuickSort bySize = new QuickSort(new Leak.ByTotalSize());
|
1999-10-15 15:56:05 +00:00
|
|
|
bySize.sort(leaks);
|
|
|
|
|
2000-09-22 07:32:52 +00:00
|
|
|
// now, print the report, sorted by type size.
|
1999-10-13 23:42:49 +00:00
|
|
|
out.println("<PRE>");
|
1999-10-15 15:56:05 +00:00
|
|
|
Type anchorType = null;
|
2000-09-22 07:32:52 +00:00
|
|
|
int leakCount = leaks.length;
|
|
|
|
for (int i = 0; i < leakCount; ++i) {
|
1999-10-08 04:12:11 +00:00
|
|
|
Leak leak = leaks[i];
|
1999-10-15 15:56:05 +00:00
|
|
|
if (anchorType != leak.mType) {
|
|
|
|
anchorType = leak.mType;
|
|
|
|
out.println("\n<HR>");
|
|
|
|
out.println("<A NAME=\"" + anchorType.mName + "_" + anchorType.mSize + "\"></A>");
|
|
|
|
out.println("<H3>" + anchorType + " Leaks</H3>");
|
|
|
|
}
|
2000-09-22 07:32:52 +00:00
|
|
|
out.println("<A NAME=\"" + leak.mName + "\"></A>");
|
1999-10-21 23:58:28 +00:00
|
|
|
if (leak.mParents != null) {
|
|
|
|
out.print(leak);
|
2000-09-22 07:32:52 +00:00
|
|
|
out.println(" <A HREF=\"#" + leak.mName + "_parents\">parents</A>");
|
1999-10-21 23:58:28 +00:00
|
|
|
} else {
|
|
|
|
out.println(leak);
|
|
|
|
}
|
1999-10-12 18:59:52 +00:00
|
|
|
// print object's fields:
|
1999-10-08 04:12:11 +00:00
|
|
|
Object[] refs = leak.mReferences;
|
|
|
|
int count = refs.length;
|
|
|
|
for (int j = 0; j < count; j++)
|
1999-12-10 20:23:16 +00:00
|
|
|
printField(out, refs[j]);
|
1999-10-12 18:59:52 +00:00
|
|
|
// print object's stack crawl:
|
2000-04-10 17:42:24 +00:00
|
|
|
in.seek(leak.mCrawlOffset);
|
2000-09-22 07:32:52 +00:00
|
|
|
short crawlCount = leak.mCrawlCount;
|
2000-04-10 17:42:24 +00:00
|
|
|
while (crawlCount-- > 0) {
|
|
|
|
String line = in.readLine();
|
|
|
|
String location = FileLocator.getFileLocation(line);
|
1999-10-12 18:59:52 +00:00
|
|
|
out.println(location);
|
|
|
|
}
|
1999-10-21 23:58:28 +00:00
|
|
|
// print object's parents.
|
|
|
|
if (leak.mParents != null) {
|
2000-09-22 07:32:52 +00:00
|
|
|
out.println("<A NAME=\"" + leak.mName + "_parents\"></A>");
|
1999-10-21 23:58:28 +00:00
|
|
|
out.println("\nLeak Parents:");
|
|
|
|
Leak[] parents = leak.mParents;
|
|
|
|
count = parents.length;
|
|
|
|
for (int j = 0; j < count; j++)
|
|
|
|
out.println("\t" + parents[j]);
|
|
|
|
}
|
1999-10-08 04:12:11 +00:00
|
|
|
}
|
1999-12-10 20:23:16 +00:00
|
|
|
out.println("</PRE>");
|
|
|
|
}
|
1999-10-07 02:11:44 +00:00
|
|
|
}
|