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;
int m_num_objs;
char* m_mem;
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;
};
int main()
{
int num = 10 000 000;
MemFact<test_class> mem_fact(num);
for(int i = 0; i < num; i++)
test_class* new_test = mem_fact.getInstance(i);
mem_fact.release();
for(int i = 0; i < num; i++)
test_class* new_test = new test_class();
}