Memory allocation problem with c++/cli
It seems that starting a managed Thread in c++/cli allocates 1MB of memory.
I created a quick and dirty sample to reproduce this behaviour, it creates
100 threads and each thread sleeps for 10 seconds. Memory usage (while all
threads are live), 100MB when compiled with the c++/cli compiler.
Same code ported and compiled with VS.NET 2003 allocates just 3 MB.
It has to be that I'm doing something wrong, can anybody put some light on
this?
Srdjan
//.net 2.0
using namespace System;
using namespace System::Threading;
using namespace System::Diagnostics;
ref class Work {
public:
static void DoWork() {
Console::WriteLine("Static thread procedure.");
Thread::Sleep(10000);
}
void DoMoreWork() {
Console::WriteLine("Instance thread procedure.");
Thread::Sleep(10000);
}
};
int main() {
ThreadStart^ threadDelegate;
Thread^ newThread;
Console::WriteLine("Put Break here...");
for (int _i=0; _i < 100; _i++) {
Work^ w = gcnew Work;
threadDelegate = gcnew ThreadStart(w, &Work::DoMoreWork );
newThread = gcnew Thread( threadDelegate );
newThread->Start();
}
Console::WriteLine("...and Put Break here");
Console::WriteLine("Press ENTER to finish");
Console::ReadLine();
}
//.net 1.1
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
using namespace System::Diagnostics;
__gc class Work {
public:
static void DoWork() {
Console::WriteLine("Static thread procedure.");
Thread::Sleep(10000);
}
void DoMoreWork() {
Console::WriteLine("Instance thread procedure.");
Thread::Sleep(10000);
}
};
int main() {
ThreadStart* threadDelegate;
Thread* newThread;
Console::WriteLine("Put Break here...");
for (int _i=0; _i <100; _i++) {
Work* w = new Work;
threadDelegate = new ThreadStart( w, &Work::DoMoreWork );
newThread = new Thread( threadDelegate );
newThread->Start();
}
Console::WriteLine("...and Put Break here");
Console::WriteLine("Press ENTER to finish");
Console::ReadLine();
}