Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling short-lived Java applications

Is there any Java profiler that allows profiling short-lived applications? The profilers I found so far seem to work with applications that keep running until user termination. However, I want to profile applications that work like command-line utilities, it runs and exits immediately. Tools like visualvm or NetBeans Profiler do not even recognize that the application was ran.

I am looking for something similar to Python's cProfile, in that the profiler result is returned when the application exits.

like image 950
ejel Avatar asked Apr 16 '10 20:04

ejel


People also ask

What is application profiling in Java?

Profiling is the process of examining an application to locate memory or performance-related issues. When profiling a Java application, you can monitor the Java Virtual Machine (JVM) and obtain data about application performance, including method timing, object allocation and garbage collection.

How do Java profilers work?

Sampling profilers work by periodically querying the JVM for all the running threads and getting the stack trace for each thread. It then determines what method each thread was executing when the sample was taken and compares the samples to determine how much time was spent in that method.

Is JVM a Java Profiler?

Java VisualVM is a very simple yet one of the best java profilers in the market. It is provided by Java Development Kit (JDK). By default, it comes bundled with the Java Development Kit (JDK). Its operation relies on some other standalone tools provided in the JDK, like JConsole, jinfo, jmap, etc.

What is memory profiling in Java?

Memory profiling enables you to understand the memory allocation and garbage collection behavior of your applications over time. It helps you identify method calls in the context within which most memory was allocated and combine this information with the number of allocated objects.


2 Answers

You can profile your application using the JVM builtin HPROF.

It provides two methods:

  1. sampling the active methods on the stack
  2. timing method execution times using injected bytecode (BCI, byte codee injection)

Sampling

This method reveals how often methods were found on top of the stack.

java -agentlib:hprof=cpu=samples,file=profile.txt ...

Timing

This method counts the actual invocations of a method. The instrumenting code has been injected by the JVM beforehand.

java -agentlib:hprof=cpu=times,file=profile.txt ...

Note: this method will slow down the execution time drastically.


For both methods, the default filename is java.hprof.txt if the file= option is not present.

Full help can be obtained using java -agentlib:hprof=help or can be found on Oracles documentation

like image 113
VHF Avatar answered Sep 27 '22 20:09

VHF


Sun Java 6 has the java -Xprof switch that'll give you some profiling data.

-Xprof            output cpu profiling data
like image 33
McDowell Avatar answered Sep 27 '22 19:09

McDowell