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;
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;
}
}
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" );
}
source
share