Java profiling – What is difference between sampling and profiling
Sampling and profiling are the two major ways of collecting various information about your running code and analysing the memory, CPU performance of your applications. So which one should be preferred and how are they different from each other?
VisualVM’s sampler, however, takes a dump of all of the threads of execution on a fairly regular basis, and uses this to work out how roughly how much CPU time each method spends.
- It takes lots of thread dumps and analyzes the stack traces.
- It is usually fast
- It does not require runtime changes in your bytecode (which may break it)
- It is also less accurate.
So if you have long running applications , high accuracy is not a requirement and you do not want to slow down your running applications, go for sampling
Profiling primarily relies on instrumenting your byte code and tweaking it.
1. It instruments your classes and methods, so they “report” whenever they are run.
2. It is more accurate, as it counts every invocation of instrumented method, not only those caught when the dump is done.
3. However instrumentation means that the bytecode of your classes is changed, and this may break your program.
4. Actually, for that reason, using profiling on large application servers (like JBoss, or WebLogic) often causes everything to die or hang.
If you do not want to compromise on accuracy and ready to sacrifice a bit of performance for few minutes, go for profiling