how I get the CPU and memory usage during the program run?

In Visual Studio, CPU and memory usage can be collected while the program runs by using the Diagnostic Tools window during debugging, or the Performance Profiler for release builds.

For debugging:

  1. Open the project in Visual Studio.
  2. Set a breakpoint where measurement should begin.
  3. Set a second breakpoint where measurement should end.
  4. Open Debug > Windows > Show Diagnostic Tools if the window is not already visible.
  5. In Select Tools, enable CPU Usage, Memory Usage, or both.
  6. Start debugging with F5.
  7. Run until the first breakpoint.
  8. While paused:
    • For CPU, open the CPU Usage tab and select Start Recording.
    • For memory, select Take snapshot on the Memory Usage toolbar.
  9. Press F5 to continue to the second breakpoint.
  10. At the second breakpoint:
  • CPU data is available in the CPU Usage tab for the code region between breakpoints.
  • Take another memory snapshot to compare memory growth.

Notes:

  • The Diagnostic Tools window appears automatically during debugging unless it was turned off.
  • For native or mixed-mode apps, memory snapshots are disabled by default. Start debugging, open the Memory Usage tab, choose Heap Profiling, then stop and restart debugging.
  • A baseline memory snapshot at the start of the session helps compare later snapshots.

For release builds without debugging:

  1. Open the project.
  2. In the Debug menu, set the solution configuration to Release.
  3. Select Debug > Performance Profiler.
  4. On the Flexible tab, select Memory Usage and choose Start.
  5. The Diagnostic Tools window shows a timeline graph of memory use while the app runs.
  6. Take snapshots during the session to capture memory state at specific moments.

If code-based process memory information is needed in a Win32 app, the PSAPI function GetProcessMemoryInfo can be used after opening the process handle with OpenProcess.


Read more here: Source link