public final class SamplingProfiler extends Object
Thread.getStackTrace to collect samples. As such, the overhead is
higher than a native approach and it does not provide insight into
where time is spent within native code, but it can still provide
useful insight into where a program is spending time.
SamplingProfiler. It samples the current thread's stack to a depth
of 12 stack frame elements over two different measurement periods
with samples taken every 100 milliseconds. In then prints the
results in hprof format to the standard output.
ThreadSet threadSet = SamplingProfiler.newArrayThreadSet(Thread.currentThread());
SamplingProfiler profiler = new SamplingProfiler(12, threadSet);
profiler.start(100);
// period of measurement
profiler.stop();
// period of non-measurement
profiler.start(100);
// another period of measurement
profiler.stop();
profiler.shutdown();
AsciiHprofWriter.write(profiler.getHprofData(), System.out);
| Modifier and Type | Class and Description |
|---|---|
static interface |
SamplingProfiler.ThreadSet
A ThreadSet specifies the set of threads to sample.
|
| Constructor and Description |
|---|
SamplingProfiler(int depth,
SamplingProfiler.ThreadSet threadSet)
Create a sampling profiler that collects stacks with the
specified depth from the threads specified by the specified
thread collector.
|
| Modifier and Type | Method and Description |
|---|---|
HprofData |
getHprofData()
Returns the hprof data accumulated by the profiler since it was
created.
|
static SamplingProfiler.ThreadSet |
newArrayThreadSet(Thread... threads)
Returns a ThreadSet for a fixed set of threads that will not
vary at runtime.
|
static SamplingProfiler.ThreadSet |
newThreadGroupThreadSet(ThreadGroup threadGroup)
Returns a ThreadSet that is dynamically computed based on the
threads found in the specified ThreadGroup and that
ThreadGroup's children.
|
void |
shutdown()
Shuts down profiling after which it can not be restarted.
|
void |
start(int interval)
Starts profiler sampling at the specified rate.
|
void |
stop()
Stops profiler sampling.
|
public SamplingProfiler(int depth,
SamplingProfiler.ThreadSet threadSet)
depth - The maximum stack depth to retain for each sample
similar to the hprof option of the same name. Any stack deeper
than this will be truncated to this depth. A good starting
value is 4 although it is not uncommon to need to raise this to
get enough context to understand program behavior. While
programs with extensive recursion may require a high value for
depth, simply passing in a value for Integer.MAX_VALUE is not
advised because of the significant memory need to retain such
stacks and runtime overhead to compare stacks.threadSet - The thread set specifies which threads to
sample. In a general purpose program, all threads typically
should be sample with a ThreadSet such as provided by newThreadGroupThreadSet. For a
benchmark a fixed set such as provided by newArrayThreadSet can reduce the overhead
of profiling.public static SamplingProfiler.ThreadSet newArrayThreadSet(Thread... threads)
newThreadGroupThreadSet(java.lang.ThreadGroup), which has
to enumerate the threads each time profiler wants to collect
samples.public static SamplingProfiler.ThreadSet newThreadGroupThreadSet(ThreadGroup threadGroup)
public void start(int interval)
interval - The number of milliseconds between samplespublic void stop()
start(int) to continue sampling.public void shutdown()
public HprofData getHprofData()