The new one loads a lot of extra memory

I am creating an application that will use many dynamically generated objects (raytracing). Instead of just using [new] over and over, I thought I was just creating a simple memory system to speed things up. It is very simple at the moment, because I do not need much.

My question is: when I run this test application, using my memory manager uses the correct amount of memory. But when I start the same cycle using [new], it uses 2.5 - 3 times more memory. Is there something I don’t see here, or are [new] huge overhead?

I am using VS 2010 on Win7. Also, I just use the task manager to view the memory usage of the process.

template<typename CLASS_TYPE>
class MemFact
{
public:
  int m_obj_size; //size of the incoming object
  int m_num_objs; //number of instances
  char* m_mem; //memory block

  MemFact(int num) : m_num_objs(num)
  {
    CLASS_TYPE t;
    m_obj_size = sizeof(t);
    m_mem = new char[m_obj_size * m_num_objs);
  }

  CLASS_TYPE* getInstance(int ID)
  {
    if( ID >= m_num_objs) return 0;
    return (CLASS_TYPE*)(m_mem + (ID * m_obj_size));
  }

  void release() { delete m_mem; m_mem = 0; }
};
/*---------------------------------------------------*/
class test_class
{
  float a,b,c,d,e,f,g,h,i,j; //10 floats
};
/*---------------------------------------------------*/
int main()
{
  int num = 10 000 000; //10 M items
  // at this point we are using 400K memory
  MemFact<test_class> mem_fact(num);
  // now we're using 382MB memory
  for(int i = 0; i < num; i++)
     test_class* new_test = mem_fact.getInstance(i);
  mem_fact.release();
  // back down to 400K
  for(int i = 0; i < num; i++)
     test_class* new_test = new test_class();
  // now we are up to 972MB memory
}
+3
2

, CRT. 16 . 12 ( , x86), , , 4 . , , , - - . , , (, ), , , .

, , - undefined. , , 16- , , 32 , . .

+4

, , - -POD . . . , . , MemFact.

, , , , . , operator new , - , delete, , . . MemFact . , new - , .

.

-2

All Articles