YourKit Java Profiler can optionally record object allocations, that is, track method calls where objects are created.
To start allocation recording, connect to the profiled application and use corresponding toolbar button:
Object allocation recording modes
Recording of allocations adds performance overhead. This is the reason why allocations should not be recorded permanently. Instead, it is recommended to record allocations only when you really need them.
Also, you may choose from two available recording modes to balance between result accuracy and fullness and profiling overhead.
Record thread and stack where objects are allocated (traditional recording)
This mode provides most detail: full stack and thread where particular object is created is obtained and remembered for each recorded object. Allocation information for particular objects in available in a memory snapshot. Also, this mode enables comprehensive analysis of excessive garbage allocation.
Memory snapshots captured when allocations are being recorded, or after object allocation recording has been stopped, contain allocation information.
If an object is created when allocations are not being recorded, or recording is restarted after the object has been created, or allocation results are explicitly cleared after the object has been created, snapshot will contain no allocation information for that object.
In order to keep moderate overhead, it is reasonable to skip allocation events for some percent of objects. This approach is useful to find the excessive garbage collection.
Also, you can record allocations for each object with size bigger than certain threshold. It is valuable to know where the biggest objects are allocated. Normally there are not so many such big objects, thus recording their allocation should not add any significant overhead.
In some rare cases you can record each created object e.g. when allocation information for some particular object must be obtained. To achieve this, set "Record each" to 1.
Count allocated objects (quick recording)
This is the most lightweight allocation recording mode.
Use this mode to quickly get insight on how many objects are created and of which classes. In particular, this identifies excessive garbage allocation problems (lots of temporary objects).
Object counting is specially designed to have minimal possible, almost zero overhead:
Use object counting to initially detect possible problems: thanks to its low overhead you may do this even in production.
Further investigation may involve using traditional allocation recording to get comprehensive profiling results with stack traces (call tree).
Start and stop recording
You can start and stop recording of allocations during execution of your application as many times as you wish. When allocations are not recorded, memory profiling adds no performance overhead to the application being profiled.
You can control recording of allocations from the profiler UI as described above, or via Profiler API. You can also record allocations from the start of application execution (see Running applications with the profiler).
Object allocation recording results in the user interface
Allocation recording is based on bytecode instrumentation, which is on by default and imposes almost no overhead while allocation recording is not running.
However, if you apply the startup options
to totally eliminate the overhead, allocation recording will not be possible.
Sampled object allocation recording option (advanced topic)
The greatest contribution to object allocation recording overhead in the traditional mode is made by obtaining exact stack trace for each recorded new object.
The idea is to estimate stack trace instead in order to reduce the overhead. It is similar to how CPU sampling works. The sampling thread periodically obtains stacks of running threads. When a thread creates a new object, the stack recently remembered for that thread is used as the object allocation stack estimation.
And just like in case of CPU sampling, the sampled object allocation recording results are relevant only for methods longer than the sampling period.
When should be used
Use this mode to get allocation hot spots, to find top methods responsible for most allocations. Don't use it if you need precise results for all recorded objects.
How to enable
Exact stack traces are gathered by default when you start object allocation recording. To use sampled stacks instead:
in the profiler UI: choose "Estimated (sampled) stacks instead of exact stacks" in the "Start Object Allocation Recording" toolbar popup window
boolean sampledAllocationRecording. The old version of the method without that parameter records exact stacks.