yychen
yychen

Reputation: 11

Memory usage by map structure in c++ program

In my following program, when I increase the length of the string list, i.e. increase the value of len1, the memory usage seen from windows task manager rises accordingly. However, when I increase the length of the map, i.e. increase the value of len2, the memory usage seen from windows task manager remains nearly the same. Could you kindly share some light over this problem? Thank you!

#include <iostream>
#include <map>
#include <stdio.h>
#include <unistd.h>
using namespace std;
void fillMemory(int i,int j,map<int,int> m);

class NewObject;
void fillMoreMemory(int i,int j, map<int,NewObject> m1);
class NewObject{
        long i = 0L;
        long j = 0L;
        long k = 0L;
    };


int main()
{

    map<int,int> m;
    map<int,NewObject> m1;
    cout << "Hello world!" << endl;
    int len1=1000;
    string l1[len1];
    for(int i=0;i<len1;i++){
        l1[i]="hello";
    }
    int len2=1000000;
    fillMemory(1,len2,m);
    fillMoreMemory(1,len2,m1);
    cout << "Hello world!" << endl;
    getchar();
    return 0;
}

void fillMemory(int i,int j, map<int,int> m){

        for(int k=i; k< j; k++){
            m.insert(pair<int,int>(i,k));
        }


    }

void fillMoreMemory(int i,int j, map<int,NewObject> m1){

        for(int k=i; k< j; k++){
            NewObject n;
            m1.insert(pair<int,NewObject>(i,n));
        }


    }

Upvotes: 1

Views: 155

Answers (2)

Zig Razor
Zig Razor

Reputation: 3535

You pass the map by value in your function, so from the point of view of the main all the changes are discarded when the function exit.

Now if you compile with some optimization probably the program never insert element in the map.To avoid this try to use the map element inserted inside the function, and see the result while the function is in execution.

However when the function exit, you can't see the memory filled more than when you start the function.

Other than this, you ever fill the first element of the map with the i index. Try to substitute i in the pair with k.

To appreciate the changes in the map, try to pass it to the function by reference or by pointer, in the following way:

#include <iostream>
#include <map>
#include <stdio.h>
#include <unistd.h>
using namespace std;
void fillMemory(int i,int j,map<int,int>& m);

class NewObject;
void fillMoreMemory(int i,int j, map<int,NewObject>& m1);
class NewObject{
        long i = 0L;
        long j = 0L;
        long k = 0L;
    };


int main()
{

    map<int,int> m;
    map<int,NewObject> m1;
    cout << "Hello world!" << endl;
    int len1=1000;
    string l1[len1];
    for(int i=0;i<len1;i++){
        l1[i]="hello";
    }
    int len2=1000000;
    fillMemory(1,len2,m);
    fillMoreMemory(1,len2,m1);
    cout << "Hello world!" << endl;
    getchar();
    return 0;
}

void fillMemory(int i,int j, map<int,int>& m){

        for(int k=i; k< j; k++){
            m.insert(pair<int,int>(k,k));
        } 

    }

void fillMoreMemory(int i,int j, map<int,NewObject>& m1){

        for(int k=i; k< j; k++){
            NewObject n;
            m1.insert(pair<int,NewObject>(k,n));
        }       
    }

In this way you will see the memory increased.

Upvotes: 3

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136525

Your map filling functions insert elements with the same key 1, which means they only insert 1 element and discard the rest, regardless of the value of len2.

Upvotes: 2

Related Questions