Skip to content

Instantly share code, notes, and snippets.

@mrzor
Created November 4, 2016 17:28
Show Gist options
  • Select an option

  • Save mrzor/4486f49eced97bba18fb32557090cd53 to your computer and use it in GitHub Desktop.

Select an option

Save mrzor/4486f49eced97bba18fb32557090cd53 to your computer and use it in GitHub Desktop.
A flawed BTrace script to record all object allocations
package btraces;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
@BTrace
public class AllAllocationsTogglable {
@TLS private static boolean enabled = false;
@OnMethod(
clazz="java.lang.System",
method="setProperty"
)
public static void onSetProperty(String key, String value) {
if (Strings.compareTo("tracerino.allAllocations", key) != 0)
return;
if (Strings.compareTo("0", value) == 0) {
enabled = false;
String traceTag = Sys.Env.property("tracerino.allAllocations");
print("AllAllocations: disabling <");
print(traceTag);
println('>');
return;
}
enabled = true;
print("AllAllocations: enabling <");
print(value);
println('>');
}
@OnMethod(clazz="+java.lang.Object",
method="<init>")
public static void onNewThing(@Self Object o,
@ProbeClassName String pcn) {
if (!enabled)
return;
Class objectClass = Reflective.classOf(o);
String objectClassString = Reflective.name(objectClass);
if (Strings.compareTo(pcn, objectClassString) == 0) {
print(pcn);
print('\t');
println(Strings.str(sizeof(o)));
}
}
}
@mrzor
Copy link
Copy Markdown
Author

mrzor commented Nov 5, 2016

Amongst other things, one pretty big flaw is that it doesn't instrument constructors that take arguments (which might be fixable).

@RoySunnySean007
Copy link
Copy Markdown

Hi Eli Zor,

Thanks for providing this awesome script, according to btraceio/btrace#252, you made some modification on Feb 2017, could u please provide updated version of this script?

My task is use Btrace to track big object allocation and its stacktrace.

Really appreciated ur help in advance!

Thanks,
Roy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment