## 4.1.3.5.1.11.2  Profile an Application Running on SapMachine with JFR/JMC
You can use Java Flight Recorder (JFR ) to profile your Java application on SapMachine, and Java Mission Control (JMC ) to do remote profiling and analysis.
## Prerequisites
- You have downloaded and installed a JDK Mission Control console. To do that, go to SapMachine → Download → JMC 9 , select your operating system, and then choose Download .
- You have installed the Cloud Foundry command line interface. See: Download and Install the cf CLI
- You are logged on to a SAP BTP, Cloud Foundry space. See: Log On to the Cloud Foundry Environment Using cf CLI
- The version of your buildpack is at least 1.110.0 (for SAP Java Buildpack 1) or 2.24.0 (for SAP Java Buildpack 2).
- The version of your SapMachine is at least 17.0.14 or 21.0.6 .
- You have a Java application that is up and running on SAP BTP, Cloud Foundry.
- You have enabled SSH for your application. See: Configuring SSH access at the app level
## Context
The commands below use async-profiler , which is a lightweight command-line tool that uses low memory resources. You can use it to execute jcmd commands without worrying about high memory consumption or potential Out-Of-Memory errors during profiling. The async-profiler tool is integrated in SapMachine, thus no need to download it.
To profile with JMC, you need to start the Management Agent on a RMI port and then open an SSH tunnel to connect to that port.
##  Note
In the steps below, we use myapp and 5555 as exemplary application name and port. Replace them with your actual app name and a free port number on your localhost.
## Procedure
1. Set the RMI server to localhost . T o do that, set the following JBP\_CONFIG\_JAVA\_OPTS option in the manifest.yml file of your application:
2. Start the Management Agent by running jcmd in your Cloud Foundry container. Execute:
```
---applications: - name: myapp buildpack: sap_java_buildpack_jakarta ... env: TARGET_RUNTIME: tomcat JBP_CONFIG_COMPONENTS: "jres: ['com.sap.xs.java.buildpack.jre.SAPMachineJRE']" JBP_CONFIG_SAP_MACHINE_JRE: '{version: 21.+}' JBP_CONFIG_JAVA_OPTS: "[java_opts: '-Djava.rmi.server.hostname=127.0.0.1']"
```
```
cf ssh myapp -c "app/META-INF/.sap_java_buildpack/sap_machine_jre/bin/asprof
```
```
jcmd $(pgrep java) ManagementAgent.start jmxremote.authenticate=false jmxremote.ssl=false jmxremote.port=5555 jmxremote.rmi.port=5555"
```
Depending on what your SapMachine is using (JRE or JDK), specify the path accordingly ( sap\_machine\_jre or sap\_machine\_jdk ). T o learn more, see: SapMachine [page 301]
## Result :
7: Command executed successfully
Management Agent is started and is listening to port 5555 .
3. (Optional) You can check the status of the Management Agent. Execute:
cf ssh myapp -c "app/META-INF/.sap\_java\_buildpack/sap\_machine\_jre/bin/asprof jcmd $(pgrep java) ManagementAgent.status"
4. Enable an SSH tunnel for this port. Execute:
cf ssh myapp -N -T -L 5555:127.0.0.1:5555
Your local port 5555 is connected to the remote port 5555 of the JVM, which is running in the Cloud Foundry container.
##  Note
The connection is active until you close the SSH tunnel. When you finish profiling, close the SSH tunnel by pressing Ctrl + C .
<!-- image -->
5. Connect with JMC to localhost:5555 . T o do that:
1. Open your JDK Mission Control (JMC) console.
2. From, the JVM Browser tab, choose icon Create a new custom JVM connection .
3. For Port , enter 5555 (or your actual port number), and then choose Finish .
6. In the left-side menu, a new node localhost:5555 appears.
7. Double-click on MBean Server .
8. To start profiling, go to Flight Recorder and from its context menu, choose Start Flight Recording .
9. Set up the event settings. From the drop-down menu, choose one of the following options (templates):
- Continuous - Low overhead configuration, safe for continuous use in production environments
- Profiling - Low overhead configuration for profiling
- gc - GC related events. Generates a small recording size.
- gc\_details - GC related events. Gets heap statistic and generates a large recording size.
- Create your own configuration. To do that, choose Template Manager , then select a predefined template and choose Duplicate . Rename the new template and edit its configuration parameters according to your needs and preferences.
10. After profiling is done, go to localhost:5555 and from its context menu, choose Disconnect .
11. Then go back to your command line and close the SSH tunnel by pressing Ctrl + C .
12. Stop the Management Agent. Execute:
cf ssh myapp -c "app/META-INF/.sap\_java\_buildpack/sap\_machine\_jre/bin/asprof jcmd $(pgrep java) ManagementAgent.stop"