简体   繁体   English

如何在内存分析过程中跟踪C ++应用程序中的内存访问频率和数量?

[英]How can I track the frequency and volume of memory access in a C++ application during memory profiling?

I'm attempting to reduce the memory footprint of a C++ application. 我正在尝试减少C ++应用程序的内存占用。 Over time this application's use of memory has grown due to developers creating new, duplicate representations of data in memory for various purposes. 随着时间的流逝,由于开发人员出于各种目的在内存中创建新的重复数据表示形式,因此该应用程序对内存的使用量有所增加。

I'm would like to determine how frequently these duplicitous representations of data are accessed so that I can decide whether or not to make them short-lived and create-on-access in order to reduce peak heap size. 我想确定访问这些重复的数据表示的频率,以便我可以决定是否使它们短暂存在并按访问创建,以减小峰值堆大小。

So my question is - what is the best way to track not only the size and volume of memory allocations, but also the frequency and volume of accesses to heap memory? 所以我的问题是-不仅跟踪内存分配的大小和数量, 而且跟踪对堆内存的访问频率和数量的最佳方法是什么 I know that all basic memory profilers handle allocation info - correlating that to memory accesses is what I'm interested in. 我知道所有基本的内存探查器都会处理分配信息-我感兴趣的是将其与内存访问相关联。

An ideal answer would be platform independent, as this application runs on Windows, Linux, iOS, and Android. 理想的答案是平台无关,因为此应用程序可在Windows,Linux,iOS和Android上运行。 However, I'll accept answers which work on any of those platforms and for any processor architecture commonly used by those platforms, as we don't have platform-specific behaviour which should impact this sort of thing. 但是,我将接受适用于任何平台以及这些平台常用的任何处理器体系结构的答案,因为我们没有特定平台的行为会影响这种情况。

Like it was commented, your question is very broad. 就像评论一样,您的问题非常广泛。

I can't answer it in a specific manner, but I'll assume that you have access to the source code, you can compile it with gcc, and your plateform supports Valgrind. 我无法以特定的方式回答它,但是我假设您可以访问源代码,可以使用gcc进行编译,并且您的平台支持Valgrind。 If my assumptions are false, please update your question, as the following is a crude tutorial on Valgrind's massif, and that was not what was asked for. 如果我的假设是错误的,请更新您的问题,因为以下是有关Valgrind地块的粗略教程,而这并不是要的。

  1. Install Valgrind 安装Valgrind
  2. Compile your program with -g and -O0 用-g和-O0编译程序
  3. Run your program with valgrind --tool=massif your.exe 使用valgrind --tool=massif your.exe运行程序
  4. Once the execution is completed, the massif tool will have created a file named massif.out.[PID] 执行完成后,massif工具将创建一个名为massif.out的文件。[PID]
  5. Run the command ms_print massif.out.[PID] 运行命令ms_print massif.out.[PID]

This will produce a graph showing the memory consumption and detailed information about all the allocation points in the program, including the point of peak memory allocation. 这将产生一个图表,显示内存消耗以及有关程序中所有分配点(包括峰值内存分配点)的详细信息。

If you want to track the access to memory, you can use the DHAT tool (see this link for detailed instructions) : 如果要跟踪对内存的访问,可以使用DHAT工具(有关详细说明,请参见此链接 ):

  1. As with massif, compile your program with -g and -O0 与massif一样,使用-g和-O0编译程序
  2. Run your program with valgrind --tool=exp-dhat your.exe 使用valgrind --tool=exp-dhat your.exe运行程序

Two points: 两点:

1) If you are looking for memory leaks (which can be very slow) the way to do that is to use one of the methods for seeing what memory blocks have not been freed when the program finishes. 1)如果您正在寻找内存泄漏(这可能非常慢),那么执行此操作的方法是使用一种方法来查看程序完成时尚未释放哪些内存块。 That allows you to figure out where they came from, and why they were not freed. 这样您就可以弄清它们来自何处,以及为什么它们没有被释放。

2) If it is a matter of excessive memory allocation (and freeing) I've found that it is accompanied by a large fraction of time spent doing that. 2)如果是过多的内存分配(和释放)问题,我发现这样做会花费大量时间 So it is not just a memory issue, it is a performance issue, and those are easy to find. 因此,这不仅是内存问题,还是性能问题,而且这些问题很容易找到。 Here's an example. 这是一个例子。

Notice, this is a little different from what you asked. 注意,这与您要求的有所不同。 You asked how to track the memory allocations, so that you could find the ones that could be eliminated. 您询问如何跟踪内存分配,以便可以找到可以消除的内存分配。 What this technique does is find them directly, without going through the tracking part. 该技术的作用是直接找到它们,而无需通过跟踪部分。 The way that works is that memory allocation and freeing is computationally expensive, so it tends to account for a large fraction of cycles, so random-time samples easily expose it. 有效的方法是内存分配和释放在计算上是昂贵的,因此它往往占很大一部分周期,因此随机时间样本很容易公开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM