Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is .NET memory management faster in managed code than in native code?

I would have thought that it would be clear cut whether memory allocation is faster in managed code than in native code - but there seems to be some controversy. Perhaps memory management under a Virtual Machine be faster because of no context-swapping calls to the OS, but then I see that the VM would itself need to make periodic calls to the OS for more memory, and it has the management overheads the memory itself rather than the OS.

Rather than making unsubstantiated assertions like I have, please provide links to references that support your position.

like image 718
Josh Avatar asked Oct 06 '08 10:10

Josh


2 Answers

Take a read of http://msdn.microsoft.com/en-us/library/ms973852.aspx

It goes into some details on how memory allocation works in .NET and briefly compares it to the C++ model.

In summary, memory allocation in .NET involves grabbing the current stack point as the object's address and adding the object's data size to the stack pointer. C++ by comparison has to search through a list of freed pointers for an area of the heap large enough for the object. In most cases therefore, .NET will be faster.

like image 146
David Arno Avatar answered Nov 07 '22 03:11

David Arno


Listen to Jeff Richter of Wintellect, he's an authority on the subject:

http://www.dotnetrocks.com/default.aspx?showNum=361

like image 39
Kev Avatar answered Nov 07 '22 04:11

Kev