让程序飞 之 内存工具:valgrind –tool=massif & massif-visualizer

之前提到过内存泄漏问题,我们有memcheck工具来检查。很爽。但是有时候memcheck工具查了没泄漏,程序一跑,内存还是狂飙。这又是什么问题。。。

其实memcheck检查的内存泄漏只是狭义的内存泄漏,或者说是严格的内存泄漏,也就是说,在程序运行的生命周期内,这部分内存你是彻底释放不了了,即,你失去了这些地址。

其实还有第二种类型的内存泄漏,就是,长期闲置的内存堆积。这部分内存虽然你还保存了地址,想要释放的时候还是能释放。关键就是你忘了释放。。。杯具啊。这种问题memcheck就不给力了。这就引出了第二个内存工具valgrind –tool=massif。

先来看一个程序:

#include <iostream>
#include <vector>
using namespace std;
 
vector< int *> va, vb;
 
void fa() {
   int *p = new int [100];
   va.push_back(p);
}
 
void fb() {
   int *p = new int [100];
   vb.push_back(p);
}
 
int main() {
 
   for ( int i=0; i<100000; i++) {
     fa();
     fb();
   }
 
   return 0;
}

程序里,我们拼命申请内存,然后放起来,不用。显然,这种程序跑起来内存肯定一路狂飙,直到内存耗尽而亡。。。

面对这种程序,我们需要一个工具来找出我们分配的内存都用到哪里了。massif就是这个工具。二话不说,先编译之:

g++ –o test –g test.cpp

交给massif运行之:

valgrind --tool=massif ./test

什么都没输出?不是的,看看当前目录,多了massif.out.****的文件吧,那才是massif真正的输出。vi打开。乱码!!!人类看不懂。其实这个输出还要经过一道工序加工我们人类才能看懂:

ms_print massif.out.*** >massif.log

打开log看看:

--------------------------------------------------------------------------------
Command:            ./mem1
Massif arguments:   (none)
ms_print arguments: massif.out.20770
--------------------------------------------------------------------------------
 
     MB
79.82^                                                                       #
      |                                                                     ::#
      |                                                                 ::@:::#
      |                                                              :::::@:::#
      |                                                          :::@:::::@:::#
      |                                                       ::::::@:::::@:::#
      |                                                    :::@:::::@:::::@:::#
      |                                                 @:::::@:::::@:::::@:::#
      |                                             ::::@:::::@:::::@:::::@:::#
      |                                          :@:::::@:::::@:::::@:::::@:::#
      |                                       ::::@:::::@:::::@:::::@:::::@:::#
      |                                   ::::::::@:::::@:::::@:::::@:::::@:::#
      |                               ::::::::::::@:::::@:::::@:::::@:::::@:::#
      |                             ::::::::::::::@:::::@:::::@:::::@:::::@:::#
      |                         ::@:::::::::::::::@:::::@:::::@:::::@:::::@:::#
      |                     ::::::@:::::::::::::::@:::::@:::::@:::::@:::::@:::#
      |                  :::::::::@:::::::::::::::@:::::@:::::@:::::@:::::@:::#
      |               @:::::::::::@:::::::::::::::@:::::@:::::@:::::@:::::@:::#
      |            :::@:::::::::::@:::::::::::::::@:::::@:::::@:::::@:::::@:::#
      |        :::::::@:::::::::::@:::::::::::::::@:::::@:::::@:::::@:::::@:::#
    0 +----------------------------------------------------------------------->Mi
      0                                                                   20.69
 
Number of snapshots: 98
Detailed snapshots: [13, 28, 51, 59, 69, 79, 89, 97 (peak)]
 
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
   0              0                0                0             0            0
   1      1,463,114              408              400             8            0
   2      1,800,331        1,285,344        1,260,768        24,576            0
   3      2,133,198        2,657,576        2,606,736        50,840            0
   4      2,348,502        3,525,648        3,459,072        66,576            0
   5      2,624,527        4,680,696        4,591,472        89,224            0
   6      2,967,727        6,116,856        5,999,472       117,384            0
   7      3,206,290        7,057,808        6,924,544       133,264            0
   8      3,416,890        7,939,088        7,788,544       150,544            0
   9      3,666,980        8,985,608        8,814,544       171,064            0
10      3,912,680       10,013,768        9,822,544       191,224            0
11      4,158,380       11,041,928       10,830,544       211,384            0
12      4,404,080       12,070,088       11,838,544       231,544            0
13      4,649,780       13,098,248       12,846,544       251,704            0
98.08% (12,846,544B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->48.04% (6,292,400B) 0x400A14: fa() (mem1.cpp:8)
| ->48.04% (6,292,400B) 0x400A6A: main (mem1.cpp:20)
|
->48.04% (6,292,000B) 0x400A3D: fb() (mem1.cpp:13)
| ->48.04% (6,292,000B) 0x400A6F: main (mem1.cpp:21)
|
->02.00% (262,144B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->02.00% (262,144B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->02.00% (262,144B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->02.00% (262,144B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.00% (131,072B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.00% (131,072B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.00% (131,072B) 0x400A52: fb() (mem1.cpp:14)
           ->01.00% (131,072B) 0x400A6F: main (mem1.cpp:21)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
14      5,019,101       14,537,472       14,262,688       274,784            0
15      5,339,391       15,877,752       15,576,688       301,064            0
16      5,549,991       16,759,032       16,440,688       318,344            0
17      5,760,591       17,640,312       17,304,688       335,624            0
18      5,971,191       18,521,592       18,168,688       352,904            0
19      6,181,791       19,402,872       19,032,688       370,184            0
20      6,392,391       20,284,152       19,896,688       387,464            0
21      6,602,991       21,165,432       20,760,688       404,744            0
22      6,813,591       22,046,712       21,624,688       422,024            0
23      7,024,191       22,927,992       22,488,688       439,304            0
24      7,234,791       23,809,272       23,352,688       456,584            0
25      7,445,391       24,690,552       24,216,688       473,864            0
26      7,655,991       25,571,832       25,080,688       491,144            0
27      7,964,871       26,864,376       26,347,888       516,488            0
28      8,147,289       27,787,696       27,263,376       524,320            0
98.11% (27,263,376B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.17% (13,107,600B) 0x400A14: fa() (mem1.cpp:8)
| ->47.17% (13,107,600B) 0x400A6A: main (mem1.cpp:20)
|
->47.17% (13,107,200B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.17% (13,107,200B) 0x400A6F: main (mem1.cpp:21)
|
->03.77% (1,048,576B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->03.77% (1,048,576B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->03.77% (1,048,576B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->03.77% (1,048,576B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->02.83% (786,432B) 0x400A29: fa() (mem1.cpp:9)
         | ->02.83% (786,432B) 0x400A6A: main (mem1.cpp:20)
         |
         ->00.94% (262,144B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
29      8,442,148       28,657,544       28,116,176       541,368            0
30      8,835,268       30,302,600       29,728,976       573,624            0
31      9,228,388       31,947,656       31,341,776       605,880            0
32      9,424,948       32,770,184       32,148,176       622,008            0
33      9,621,508       33,592,712       32,954,576       638,136            0
34      9,818,068       34,415,240       33,760,976       654,264            0
35     10,014,628       35,237,768       34,567,376       670,392            0
36     10,211,188       36,060,296       35,373,776       686,520            0
37     10,407,748       36,882,824       36,180,176       702,648            0
38     10,604,308       37,705,352       36,986,576       718,776            0
39     10,800,868       38,527,880       37,792,976       734,904            0
40     10,997,428       39,350,408       38,599,376       751,032            0
41     11,193,988       40,172,936       39,405,776       767,160            0
42     11,390,548       40,995,464       40,212,176       783,288            0
43     11,587,108       41,817,992       41,018,576       799,416            0
44     11,783,668       42,640,520       41,824,976       815,544            0
45     11,980,228       43,463,048       42,631,376       831,672            0
46     12,176,788       44,285,576       43,437,776       847,800            0
47     12,373,348       45,108,104       44,244,176       863,928            0
48     12,569,908       45,930,632       45,050,576       880,056            0
49     12,864,748       47,164,424       46,260,176       904,248            0
50     13,047,168       47,927,792       47,008,576       919,216            0
51     13,229,593       48,691,160       47,756,976       934,184            0
98.08% (47,756,976B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.96% (23,354,400B) 0x400A14: fa() (mem1.cpp:8)
| ->47.96% (23,354,400B) 0x400A6A: main (mem1.cpp:20)
|
->47.96% (23,354,000B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.96% (23,354,000B) 0x400A6F: main (mem1.cpp:21)
|
->02.15% (1,048,576B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->02.15% (1,048,576B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->02.15% (1,048,576B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->02.15% (1,048,576B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.08% (524,288B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.08% (524,288B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.08% (524,288B) 0x400A52: fb() (mem1.cpp:14)
           ->01.08% (524,288B) 0x400A6F: main (mem1.cpp:21)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
52     13,412,013       49,454,528       48,505,376       949,152            0
53     13,594,438       50,217,896       49,253,776       964,120            0
54     13,776,858       50,981,264       50,002,176       979,088            0
55     13,959,283       51,744,632       50,750,576       994,056            0
56     14,141,703       52,508,000       51,498,976     1,009,024            0
57     14,324,128       53,271,368       52,247,376     1,023,992            0
58     14,506,548       54,034,736       52,995,776     1,038,960            0
59     14,797,117       55,574,960       54,526,352     1,048,608            0
98.11% (54,526,352B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.17% (26,214,800B) 0x400A14: fa() (mem1.cpp:8)
| ->47.17% (26,214,800B) 0x400A6A: main (mem1.cpp:20)
|
->47.17% (26,214,400B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.17% (26,214,400B) 0x400A6F: main (mem1.cpp:21)
|
->03.77% (2,097,152B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->03.77% (2,097,152B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->03.77% (2,097,152B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->03.77% (2,097,152B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->02.83% (1,572,864B) 0x400A29: fa() (mem1.cpp:9)
         | ->02.83% (1,572,864B) 0x400A6A: main (mem1.cpp:20)
         |
         ->00.94% (524,288B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
60     14,979,577       55,614,528       54,565,152     1,049,376            0
61     15,162,002       56,377,896       55,313,552     1,064,344            0
62     15,344,422       57,141,264       56,061,952     1,079,312            0
63     15,526,847       57,904,632       56,810,352     1,094,280            0
64     15,709,267       58,668,000       57,558,752     1,109,248            0
65     15,891,692       59,431,368       58,307,152     1,124,216            0
66     16,074,112       60,194,736       59,055,552     1,139,184            0
67     16,256,537       60,958,104       59,803,952     1,154,152            0
68     16,438,957       61,721,472       60,552,352     1,169,120            0
69     16,621,382       62,484,840       61,300,752     1,184,088            0
98.10% (61,300,752B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.37% (29,602,000B) 0x400A14: fa() (mem1.cpp:8)
| ->47.37% (29,602,000B) 0x400A6A: main (mem1.cpp:20)
|
->47.37% (29,601,600B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.37% (29,601,600B) 0x400A6F: main (mem1.cpp:21)
|
->03.36% (2,097,152B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->03.36% (2,097,152B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->03.36% (2,097,152B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->03.36% (2,097,152B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.68% (1,048,576B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.68% (1,048,576B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.68% (1,048,576B) 0x400A52: fb() (mem1.cpp:14)
           ->01.68% (1,048,576B) 0x400A6F: main (mem1.cpp:21)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
70     16,803,802       63,248,208       62,049,152     1,199,056            0
71     16,986,227       64,011,576       62,797,552     1,214,024            0
72     17,168,647       64,774,944       63,545,952     1,228,992            0
73     17,351,072       65,538,312       64,294,352     1,243,960            0
74     17,533,492       66,301,680       65,042,752     1,258,928            0
75     17,715,917       67,065,048       65,791,152     1,273,896            0
76     17,898,337       67,828,416       66,539,552     1,288,864            0
77     18,080,762       68,591,784       67,287,952     1,303,832            0
78     18,263,182       69,355,152       68,036,352     1,318,800            0
79     18,445,607       70,118,520       68,784,752     1,333,768            0
98.10% (68,784,752B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.55% (33,344,000B) 0x400A14: fa() (mem1.cpp:8)
| ->47.55% (33,344,000B) 0x400A6A: main (mem1.cpp:20)
|
->47.55% (33,343,600B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.55% (33,343,600B) 0x400A6F: main (mem1.cpp:21)
|
->02.99% (2,097,152B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->02.99% (2,097,152B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->02.99% (2,097,152B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->02.99% (2,097,152B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.50% (1,048,576B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.50% (1,048,576B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.50% (1,048,576B) 0x400A52: fb() (mem1.cpp:14)
           ->01.50% (1,048,576B) 0x400A6F: main (mem1.cpp:21)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
80     18,628,027       70,881,888       69,533,152     1,348,736            0
81     18,810,452       71,645,256       70,281,552     1,363,704            0
82     18,992,872       72,408,624       71,029,952     1,378,672            0
83     19,175,297       73,171,992       71,778,352     1,393,640            0
84     19,357,717       73,935,360       72,526,752     1,408,608            0
85     19,540,142       74,698,728       73,275,152     1,423,576            0
86     19,722,562       75,462,096       74,023,552     1,438,544            0
87     19,904,987       76,225,464       74,771,952     1,453,512            0
88     20,087,407       76,988,832       75,520,352     1,468,480            0
89     20,269,832       77,752,200       76,268,752     1,483,448            0
98.09% (76,268,752B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.70% (37,086,000B) 0x400A14: fa() (mem1.cpp:8)
| ->47.70% (37,086,000B) 0x400A6A: main (mem1.cpp:20)
|
->47.70% (37,085,600B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.70% (37,085,600B) 0x400A6F: main (mem1.cpp:21)
|
->02.70% (2,097,152B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->02.70% (2,097,152B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->02.70% (2,097,152B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->02.70% (2,097,152B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.35% (1,048,576B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.35% (1,048,576B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.35% (1,048,576B) 0x400A52: fb() (mem1.cpp:14)
           ->01.35% (1,048,576B) 0x400A6F: main (mem1.cpp:21)
--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
90     20,452,252       78,515,568       77,017,152     1,498,416            0
91     20,634,677       79,278,936       77,765,552     1,513,384            0
92     20,817,097       80,042,304       78,513,952     1,528,352            0
93     20,999,522       80,805,672       79,262,352     1,543,320            0
94     21,181,942       81,569,040       80,010,752     1,558,288            0
95     21,364,367       82,332,408       80,759,152     1,573,256            0
96     21,546,787       83,095,776       81,507,552     1,588,224            0
97     21,690,735       83,697,168       82,097,152     1,600,016            0
98.09% (82,097,152B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.79% (40,000,000B) 0x400A14: fa() (mem1.cpp:8)
| ->47.79% (40,000,000B) 0x400A6A: main (mem1.cpp:20)
|
->47.79% (40,000,000B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.79% (40,000,000B) 0x400A6F: main (mem1.cpp:21)
|
->02.51% (2,097,152B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->02.51% (2,097,152B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->02.51% (2,097,152B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->02.51% (2,097,152B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.25% (1,048,576B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.25% (1,048,576B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.25% (1,048,576B) 0x400A52: fb() (mem1.cpp:14)
           ->01.25% (1,048,576B) 0x400A6F: main (mem1.cpp:21)

不要抓狂,听我给你解释这个输出是什么意思。

massif工作的时候会每隔一定时间对程序的内存使用取个快照。就是上面的一段:

--------------------------------------------------------------------------------
   n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
60     14,979,577       55,614,528       54,565,152     1,049,376            0
61     15,162,002       56,377,896       55,313,552     1,064,344            0
62     15,344,422       57,141,264       56,061,952     1,079,312            0
63     15,526,847       57,904,632       56,810,352     1,094,280            0
64     15,709,267       58,668,000       57,558,752     1,109,248            0
65     15,891,692       59,431,368       58,307,152     1,124,216            0
66     16,074,112       60,194,736       59,055,552     1,139,184            0
67     16,256,537       60,958,104       59,803,952     1,154,152            0
68     16,438,957       61,721,472       60,552,352     1,169,120            0
69     16,621,382       62,484,840       61,300,752     1,184,088            0
98.10% (61,300,752B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->47.37% (29,602,000B) 0x400A14: fa() (mem1.cpp:8)
| ->47.37% (29,602,000B) 0x400A6A: main (mem1.cpp:20)
|
->47.37% (29,601,600B) 0x400A3D: fb() (mem1.cpp:13)
| ->47.37% (29,601,600B) 0x400A6F: main (mem1.cpp:21)
|
->03.36% (2,097,152B) 0x40142F: __gnu_cxx::new_allocator<int*>::allocate(unsigned long, void const*) (new_allocator.h:89)
   ->03.36% (2,097,152B) 0x40121B: std::_Vector_base<int*, std::allocator<int*> >::_M_allocate(unsigned long) (stl_vector.h:140)
     ->03.36% (2,097,152B) 0x400DC5: std::vector<int*, std::allocator<int*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int**, std::vector<int*, std::allocator<int*> > >, int* const&) (vector.tcc:322)
       ->03.36% (2,097,152B) 0x400BC4: std::vector<int*, std::allocator<int*> >::push_back(int* const&) (stl_vector.h:741)
         ->01.68% (1,048,576B) 0x400A29: fa() (mem1.cpp:9)
         | ->01.68% (1,048,576B) 0x400A6A: main (mem1.cpp:20)
         |
         ->01.68% (1,048,576B) 0x400A52: fb() (mem1.cpp:14)
->01.68% (1,048,576B) 0x400A6F: main (mem1.cpp:21)

这里我们主要看下半部分,它按照函数调用数的格式输出了每个函数里面每个内存分配点总共分配了多少内存。如上面:函数堆共分配了整个程序的98.10%内存,其中,mem1.cpp第8行的fa() 分配了47.37%,13行的fb也是47.3%。好了,我们知道了,所有的内存都用这俩地方了。。。果断优化。

还有更给力的工具吗?(显然,既然敢提出这个问题,就敢回答yes)

有!massif-visualizer,其实就是massif的图形前端。安装后用它打开刚刚的massif.out.xxxx效果如下:

太爽了!