Why does a VC ++ program that stores 5 MB of data consume 64 MB of system memory?

I worked on trying to figure out why my program consumes so much system RAM. I load a file from disk into a vector of structures from several dynamically allocated arrays. A 16 MB file ultimately consumes 280 MB of RAM according to the task manager. Types in a file are mostly short-short and multiple-length characters. The file contains 331,000 entries, containing an average of about 5 fields. I converted the vector to a structure and reduced the memory to 255 MB, but it still seems very high. When a vector takes up so much memory, the program runs out of memory, so I need to find a way to use the memory more intelligently.

I wrote a simple program to simply populate a vector (or array) with thousands of char pointers. I would expect it to allocate 4 + 1 bytes for each, giving up 5 MB of memory needed for storage, but actually it uses 64 MB (array version) or 67 MB (vector version). When the program starts over, it consumes only 400 thousand. So why is there an additional 59 MB for the array or 62 MB for the selected vectors? This extra memory seems to be suitable for each container, so if I create the file size_check2 and copy everything and run it, the program uses 135 MB for pointers and 10 MB data.

Thanks in advance,

size_check.h

#pragma once

#include <vector>

class size_check
{
public:
    size_check(void);
    ~size_check(void);

    typedef unsigned long   size_type;

    void stuff_me( unsigned int howMany );

private:
    size_type**         package;
//  std::vector<size_type*> package;
    size_type*          me;
};

size_check.cpp

#include "size_check.h"

size_check::size_check(void)
{
}

size_check::~size_check(void)
{
}

void size_check::stuff_me( unsigned int howMany )
{
    package = new size_type*[howMany];
    for( unsigned int i = 0; i < howMany; ++i )
    {

        size_type *me = new size_type;
        *me = 33;
        package[i] = me;
//      package.push_back( me );
    }
}

main.cpp #include "size_check.h"

int main( int argc, char * argv[ ] )
{
    const unsigned int buckets = 20;
    const unsigned int size = 50000;

    size_check* me[buckets];

    for( unsigned int i = 0; i < buckets; ++i )
    {
        me[i] = new size_check();
        me[i]->stuff_me( size );
    }
    printf( "done.\n" );
}
+5
source share
4

VS2010 52 500 . 20,944 .

, - , , .

, , .

+3

package = new size_type[howMany]; // instantiate 50,000 size_type's
for( unsigned int i = 0; i < howMany; ++i )
{
    size_type *me = new size_type; // Leak: results in an extra 50k size_type being instantiated
    *me = 33;
    package[i] = *me;  // Set a non-pointer to what is at the address of pointer "me"
    // Would package[i] = 33; not suffice?
}

, ,

+1

, , .

void size_check::stuff_me( unsigned int howMany )
{

howMany= 50000.

package = new size_type[howMany];

, 32- , 50 000 * 4 .

for( unsigned int i = 0; i < howMany; ++i )
{
    size_type *me = new size_type;

. 50 000, , 50 000 * 4 .

        *me = 33;
        package[i] = *me;
    }
}

, stuff_me() 20 main(), ~ 8 . 64- , , sizeof(long) == 8bytes, .

- , VS . , - new , , .

FYI, mingw-gcc 4.5.2, ~ 20 - , , . stuff_me :

void size_check::stuff_me( unsigned int howMany )
{
    package = new size_type[howMany];
    size_type *me = new size_type;
    for( unsigned int i = 0; i < howMany; ++i )
    {
        *me = 33;
        package[i] = *me;
    }
    delete me;
}

~ 4-5 .

+1

, , . , . - _CrtMemBlockHeader 32 . - noMansLand ( ) 4 , 36 . char 37 . 1/2, , , /malloc-.

So my job is to allocate a large block of memory to store the file in memory. Then analyze the memory image filling the pointer vector at the beginning of each record. Then, on demand, I create a record from the memory image, using the pointer to the beginning of the selected record. This reduced memory to 25 MB.

Thanks for your help and suggestions.

+1
source

All Articles