有没有办法知道当前程序的总内存消耗?

时间:2015-07-23 00:09:21

标签: c++ c++11 memory memory-management

注意:该程序只是一个例子,主要问题就在此之后。

假设我有ActiveRecord::Schema.define(version: 20150721133516) do create_table "directions", force: :cascade do |t| t.text "step" t.integer "recipe_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "directions", ["recipe_id"], name: "index_directions_on_recipe_id" create_table "ingredients", force: :cascade do |t| t.string "name" t.integer "recipe_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "ingredients", ["recipe_id"], name: "index_ingredients_on_recipe_id" create_table "recipes", force: :cascade do |t| t.string "title" t.text "description" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "image_file_name" t.string "image_content_type" t.integer "image_file_size" t.datetime "image_updated_at" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end 这样的程序:

r1 = Recipe.create(
title: "Red, White and Blue Poke Cake", 
description: "Red, white and beautiful! A simple cake-mix cake becomes the hit of your Fourth of July gathering with just a few easy additions.", 
image: "http://s3.amazonaws.com/gmi-digital-library/665dd965-1b01-46ea-8f32-4b745b037817.jpg",
user_id: "1",
image_file_name: "name",
image_content_type: "/\Aimage\/.*\Z/",
image_file_size: "400x400#",
 )

r1.ingredients.create(name: "1
box Betty Crocker™ SuperMoist™ white cake mix
Water, vegetable oil and egg whites called for on cake mix box
1
box (4-serving size) strawberry-flavored gelatin
1 cup milk", recipe_id: r1.id )
r1.directions.create(step: "1 Heat oven to 350°F (325°F for dark or    nonstick pan). Make and bake cake mix as directed on box for 13x9-inch pan. Cool completely in pan, about 1 hour.
2 Pierce cooled cake with fork at 1/2-inch intervals. In medium bowl, stir gelatin and boiling water until dissolved. Stir in cold water. Carefully pour mixture over entire surface of cake. Refrigerate at least 3 hours until serving time.
3 In large bowl, mix pudding mix and milk until well blended. Gently stir in whipped topping. Spread over cake. Arrange strawberries and blueberries on top of cake to look like flag. Store loosely covered in refrigerator.",
recipe_id: r1.id )

输出结果为:

C++

从内存地址和增加的值中可以清楚地看到#include<iostream> #include<vector> using namespace std; int main() { vector<int>numbers = {4,5,3,2,5,42}; cout<<"-------------------\n"; for (auto x : numbers){ cout<< &x <<endl; x+=10; } cout<<"-------------------\n"; for (vector<int>::iterator it = numbers.begin(); it!=numbers.end(); it++){ cout<< &(*it) <<" "<< *it << endl; } return 0; } 每次都使用变量------------------- 0x28fed4 0x28fed4 0x28fed4 0x28fed4 0x28fed4 0x28fed4 ------------------- 0x3b21a8 4 0x3b21ac 5 0x3b21b0 3 0x3b21b4 2 0x3b21b8 5 0x3b21bc 42 ,这是在新内存中。

现在,我想知道,有没有办法知道(内置函数或类似的东西):

  • 程序从执行开始到结束使用了多少内存?
  • 它使用的最大内存是多少?
  • 目前使用多少内存?

我在 Windows 8.1中的 Code :: Blocks 13.12 中使用C ++

1 个答案:

答案 0 :(得分:5)

使用内存分析器。

在Linux上,例如使用valgrind --tool=massif

演示:

--------------------------------------------------------------------------------
Command:            ./test
Massif arguments:   (none)
ms_print arguments: massif.out.26621
--------------------------------------------------------------------------------


     B
   40^                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
   0 +----------------------------------------------------------------------->Mi
     0                                                                   1.397

Number of snapshots: 4
 Detailed snapshots: [2 (peak)]

--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  0              0                0                0             0            0
  1      1,425,892               40               24            16            0
  2      1,464,762               40               24            16            0
60.00% (24B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->60.00% (24B) 0x400B2D: main (new_allocator.h:104)

--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  3      1,464,762                0                0             0            0

enter image description here