logo
On this page
2023-12-21

In Windows application development, how to analyze problems such as high CPU usage and memory leaks?

Products / Plugins:Video Call / Audio Call / Live streaming
Platform / Framework:Windows

In Windows application development, when encountering high CPU usage and memory leaks, you need to optimize and analyze the application. The following introduces two performance analysis tools. Developers can choose based on their business scenarios.

VS Performance Analysis Tool

Note

The following only introduces the basic usage of VS's built-in performance analysis tool. Developers interested can learn more about the usage of other features on their own.

Visual Studio comes with performance analysis tools that can directly analyze projects executed in VS, or track generated executable files (such as pdb files). The operations of the two have some differences when performing analysis, but the rest are the same.

Different versions of VS may differ, but the operation steps are roughly the same. The following uses Visual Studio 2015 as an example.

  1. Perform performance analysis and generate analysis report. In the menu bar, select "Debug > Performance Profiler", or use the shortcut key Alt + F2, as shown below:

    A dialog box will pop up as follows:

  2. Select to analyze existing projects, other projects, or other executable files. Taking the ZegoLiveRoomTopics sample topic Demo as an example, select existing project and click "Start" to launch.

CPU Performance Analysis

  1. In the following dialog box, select "CPU Sampling (Recommended)" and click "Finish".

    The sample Demo will automatically pop up, as shown:

  2. Taking the publishing function as an example, perform analysis.

    1. Click "Publishing" on the left.
    2. Then click "Initialize SDK", enter room roomID, and log into the room.
    3. Enter stream streamID and start publishing.
  3. At this time, close the program and wait for the analysis report to be generated. The final report is as follows:

  4. Click "Hot Path" in the middle and filter by function name.

  5. Click "Call Tree" to view the entire process.

    • Exclusive Samples: Refers to the execution time of the function that does not include subfunctions.
    • Inclusive Samples: Refers to the total execution time including subfunctions.

    The following figure shows the functions from initialization to publishing that mainly consume CPU. Among them, AVE is an internal method of ZegoLiveRoom.dll and requires importing symbol files.

    From the above figure, you can see that in this publishing process, CPU consumption, in addition to the main thread, also includes the SDK's encoding and decoding threads and other threads.

Memory Analysis

  1. Select "Debug > Windows > Show Diagnostic Tools" and check Memory Usage to see the memory running status.

    Taking the publishing function as an example. Before initialization, take the first snapshot 1; after successful publishing, take snapshot 2.

    You can see that after successful publishing, memory increased by more than 20 MB.

  2. Click the red arrow of snapshot 2 to view detailed memory growth comparison.

    You can see that the main memory growth is on unsigned char[] and void objects. From this, we can infer that the main memory growth is caused by the audio and video data module.

  3. In the upper right corner, switch View Mode to "Stack View" to view the specific trigger information of memory growth.

    You can see that the main memory growth is caused by "External Frames".

  4. Click "External Frames" to expand detailed information.

    You can see that memory growth mainly comes from ZegoLiveRoom.dll, which is consistent with the CPU growth analysis above.

Analyzing CPU Usage with Process Explorer

Process Explorer is a free tool provided by Microsoft that can easily view the CPU usage of each thread under a process for performance analysis.

  1. Open Process Explorer to view the resource status of each process.

    You can see that the CPU usage of zegoLiveRoomWrapper.exe is 3.52%.

  2. Right-click on the process and select "Properties...", then click to switch to the "Threads" tab to view specific thread usage information.

    You can see that the thread with TID 27092 has the highest CPU usage.

  3. Select the thread and click "Stack" in the lower right corner to view the running stack of the thread at this time.

    You can see that the external capture thread is mainly running to push screen data.

Using UMDH to Locate Memory Leaks

UMDH (User-Mode Dump Heap) is a lightweight memory leak analysis tool used to monitor memory usage of Windows programs.

UMDH is a tool in Debugging Tools for Windows, mainly discovering memory leaks by analyzing and comparing the Heap Stacktrace information of processes.

  1. Install Debugging Tools for Windows.

  2. Set environment variables.

    Open "Control Panel > Advanced System Settings > Environment Variables" and add the installation path of Debugging Tools for Windows (for example: "C:\Program Files (x86)\Windows Kits\10\Debuggers\x64") to the system's PATH environment variable.

  3. Set the _NT_SYMBOL_PATH system symbol path.

    set _NT_SYMBOL_PATH=c:\mysymbols;srv*c:\mycache*https://msdl.microsoft.com/download/symbols
  4. Start the application to be monitored on Windows.

  5. Generate heap snapshot.

    1. Open terminal and execute command umdh -p:PID -f:filename1.txt.

      • PID is the process ID of your application.
      • filename1.txt is the filename after saving the heap snapshot.
    2. After the program runs for a while, generate another heap snapshot by executing umdh -p:PID -f:filename2.txt.

  6. Compare the two heap snapshots.

    Execute umdh -d filename1.txt filename2.txt > diff.txt. UMDH will automatically compare the two snapshots and output the results in the "diff.txt" file.

  7. Open the diff.txt file to see the heap stack information with the largest memory growth and the corresponding leaked call functions.

If you find that memory leaks are related to ZegoExpressEngine SDK, you can save the diff.txt file and contact ZEGOCLOUD Technical Support for further analysis.

For more UMDH usage instructions, please refer to UMDH Introduction, Using UMDH to Find User-Mode Memory Leaks.

Previous

In Android platform development, how to analyze the problem of high CPU usage and device overheating?

Next

How to perform mute/unmute, mute audio, and disable camera operations on remote users?

On this page

Back to top