Set the environment variable in the window where the Rust program is running
1
exportMALLOC_CONF="prof:true,lg_prof_interval:26"
Analyze the heap
Once the code is running, you will see many jeprof.x.x.x.heap files generated in the working directory. In the example below, we use the heap generated when the program first started as the baseline and compare it with the 279th generated file. You can then open a.pdf to view the results.
The PDF clearly shows where the most memory is allocated and freed. These are the areas you can optimize.
Summary
This is only a brief introduction to the tools I recently used for CPU and memory profiling. If needed, I will add more details, such as how to read CPU flame graphs and how to inspect CPU usage.