Profiling Java code

This page describes setting up Stackdriver Profiler for profiling Java code. For Java, the Profiler offers CPU, heap, and wall-time profiling; see Profiling Concepts for more information.

You must use version 7, 8, 9 or 11 of either the OpenJDK or the Oracle JDK.

Setting up Profiler typically involves installing the profiling agent and loading it when you start Java, providing configuration values for the agent as arguments. If you are using Java, you can use the profiling agent on Linux in the following environments:

  • Compute Engine
  • Google Kubernetes Engine
  • App Engine flexible environment
  • App Engine standard environment (requires App Engine SDK version 1.9.64 or later)

You can also profile Java code on non-Google Cloud Platform systems. See Profiling Outside Google Cloud Platform for more information.

Enabling the Profiler API

Before you use the profiling agent, ensure that the underlying Profiler API is enabled. You can check the status of the API and enable it if necessary by using either the Cloud SDK gcloud command-line tool or the Cloud Console:

Cloud SDK

  1. If you have not already installed the Cloud SDK on your workstation, see Google Cloud SDK.

  2. Run the following command:

    gcloud services enable

For more information, see gcloud services.

Cloud Console

  1. Go to the APIs & Services dashboard:

    Go to APIs & services

  2. Select the project you will use to access the API.

  3. Click the Add APIs and Services button.

    Add APIs and Services

  4. Search for Profiler API.

  5. In the search results, select Stackdriver Profiler API.

  6. If API enabled is displayed, then the API is already enabled. If not, click the Enable button.

Installing the Profiler agent

Compute Engine

Create a directory, for example, /opt/cprof, in which to install Profiler:

sudo mkdir -p /opt/cprof

Download the agent archive from the repository and extract it into the installation directory:

wget -q -O- \
| sudo tar xzv -C /opt/cprof


Modify the service container Dockerfile to create a directory in which to install Profiler, download the agent archive, and extract it into the installation directory:

RUN mkdir -p /opt/cprof && \
    wget -q -O- \
    | tar xzv -C /opt/cprof

App Engine flexible environment

The Profiler agent is included in the base Java images for Java 8 and Java 9, so there is nothing to install.

App Engine standard environment

The Profiler agent is included as part of the Java 8 runtime support, so there is nothing to install.

Loading the Profiler agent

To profile your code, start Java as you normally would to run your program, but specify the agent-configuration options. You specify the path to the agent library, and you can pass options to the library.

For the App Engine standard environment, the agent is automatically loaded and configured. Skip ahead to Starting your program, for details on configuring, and starting, your program.

Agent configuration

Use the -agentpath option to specify the path to the agent library in the installation directory, and pass additional configuration to the library.

The syntax looks like this:


The most common options include:

  • -cprof_service: A name for the service being profiled. For naming restrictions, go to Service name and version arguments.
  • -cprof_service_version: (optional) The version of the service being profiled

To profile your code, add the -agentpath configuration to your regular Java invocation. For example, to run myApp.jar and profile it, a minimal invocation might be:

 java -agentpath:/opt/cprof/ -jar myApp.jar

Heap profiling

To enable heap profiling for Java 11, add -cprof_enable_heap_sampling to your -agentpath. For example, to run myapp.jar and profile it with heap profiling enabled, your invocation might be:

 java -agentpath:/opt/cprof/,-cprof_enable_heap_sampling -jar myapp.jar

Go to Found a bug in a Stackdriver product? for a list of known bugs and for information on filing a bug.

Service name and version arguments

When you load the Profiler agent, you specify a service-name argument and an optional service-version argument to configure it.

The service name lets Profiler collect profiling data for all replicas of that service. The profiler service ensures a collection rate of one profile per minute, on average, for each service name across each combination service versions and zones.

For example, if you have a service with two versions running across replicas in three zones, the profiler will create an average of 6 profiles per minute for that service.

If you use different service names for your replicas, then your service will be profiled more often than necessary, with a correspondingly higher overhead.

When selecting a service name:

  • Choose a name that clearly represents the service in your application architecture. The choice of service name is less important if you only run a single service or application. It is more important if your application runs as a set of micro-services, for example.

  • Make sure to not use any process-specific values, like a process ID, in the service-name string.

  • The service-name string must match this regular expression:


A good guideline is to use a static string like imageproc-service as the service name.

The service version is optional. If you specify the service version, Profiler can aggregate profiling information from multiple instances and display it correctly. It can be used to mark different versions of your services as they get deployed. The Profiler UI lets you filter the data by service version; this way, you can compare the performance of older and newer versions of the code.

The value of the service-version argument is a free-form string, but values for this argument typically look like version numbers, for example, 1.0.0 or 2.1.2.

Starting your program

Compute Engine

Start Java as you normally would to run your program, and add the the agent-configuration options:

java \
    -agentpath:/opt/cprof/,-cprof_service_version=1.0.0 \


Modify the service container Dockerfile to start Java as you normally would to run your program, and add the agent-configuration options:

RUN java \
    -agentpath:/opt/cprof/,-cprof_service_version=1.0.0 \

App Engine flexible environment

Modify the app.yaml configuration file to set the PROFILER_ENABLE environment variable. Then start your program as usual:


See Defining environment variables for more information.

App Engine standard environment

Modify the app.yaml or the appengine-web.xml configuration file to include the GAE_PROFILER_MODE environment variable to instruct Profiler to collect CPU and heap profiles, one time per minute on average, across all instances of the same deployment:


   GAE_PROFILER_MODE: cpu,heap


    <env-var name="GAE_PROFILER_MODE" value="cpu,heap" />

Then start your program as usual.

See Defining environment variables for more information.

Agent logging

For Compute Engine, GKE, and App Engine flexible environments, the profiling agent can report debug information in its logs. To enable, append the -logtostderr flag to the -agentpath configuration:

 java -agentpath:/opt/cprof/,-logtostderr -jar myApp.jar

What's next

To learn about the Profiler graph and controls, go to Using the Stackdriver Profiler Interface. For advanced information, go to the following:

Оцените, насколько информация на этой странице была вам полезна:

Оставить отзыв о...

Текущей странице
Stackdriver Profiler
Нужна помощь? Обратитесь в службу поддержки.