YourKit Java Profiler can optionally record object allocations, that is, track method call stacks where objects are created.
Memory snapshots captured when allocations are being recorded, or after 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.
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.
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.
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.
Allocation recording results in the user interface
Sampled allocation recording option
The greatest contribution to allocation recording overhead 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 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 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
- if allocation recording is started with alloceach or allocsizelimit startup options, specify startup option allocsampled
use API method
boolean sampledAllocationRecording. The old version of the method without that parameter records exact stacks.