Is new & free in CLR heap fast?

H

Hyun-jik Bae

I am a newbie in C# programming. I've been working in C++ language for some
years.
The new/delete statement in C++ is rather slow so it is recommended using
static data structure in some cases which require very fast performance as
you know. However, there's no way but using value type class in C# for
avoiding heap access as far as I know. I think C# is very slow unless it has
something which enables CLR very fast in accessing CLR heap memory.

The best way is to just test the new/delete performance of C# by myself,
however, it is very difficult testing every case I can imagine. Please
reply. Thanks in advance.


Hyun-jik Bae
 
C

Carl Daniel [VC++ MVP]

Hyun-jik Bae said:
I am a newbie in C# programming. I've been working in C++ language
for some years.
The new/delete statement in C++ is rather slow so it is recommended
using static data structure in some cases which require very fast
performance as you know. However, there's no way but using value type
class in C# for avoiding heap access as far as I know. I think C# is
very slow unless it has something which enables CLR very fast in
accessing CLR heap memory.
The best way is to just test the new/delete performance of C# by
myself, however, it is very difficult testing every case I can
imagine. Please reply. Thanks in advance.

First, you must realize that the CLR uses a generational heap. In the
current implementation, the heap is divided into 3 generations, called Gen0,
Gen1 and Gen2 (there's also a Large Block heap, but it doesn't enter into
the picture unless you're allocating large objects, in which case other
considerations generally swamp the time required to allocate). Allocations
are made from the Gen0 heap and promoted to Gen1 and Gen2 if the object
stays active long enough.

Allocation from the Gen0 heap is about 2 instructions (seriously). The Gen0
heap is a simple mark/release heap for which allocation simply involves
subtracting the allocation size from the marker and returning the resulting
pointer. Free is accomplished by moving all live Gen0 objects into the Gen1
heap and then completely freeing the Gen0 heap by moving the marker back to
the top.

Gen1 and Gen2 heaps are closer to C++ heap in that they have relatively
expensive allocators and deallocators. On top of that, objects in the Gen1
and Gen2 heaps can be moved by the GC during heap compaction.

Generally, short-lived objects never leave the Gen0 heap. Since over 90% of
the objects used in a typical program are short-lived, and the performance
of the Gen0 heap is comparable to that of stack-based allocation, the CLR
can actually give better performance than an optimized C++ program in some
situations. On average, it's within about 15% of the same performance.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top