views:

239

answers:

5

We have certain application written in C# and we would like it to stay this way. The application manipulates many small and short lived chunks of dynamic memory. It also appears to be sensitive to GC interruptions.

We think that one way to reduce GC is to allocate 100K chunks and then allocate memory from them using a custom memory manager. Has anyone encountered custom memory manager implementations in C#?

+4  A: 

Memory management of all types that descend from System.Object is performed by the garbage collector (with the exception of structures/primitives stored on the stack). In Microsoft's implementation of the CLR, the garbage collector cannot be replaced.

You can allocate some primitives on the heap manually inside of an unsafe block and then access them via pointers, but it's not recommended.

Likely, you should profile and migrate classes to structures accordingly.

rpetrich
+1 for migrate the classes to structures.If you do need structural inheritance consider using the forceOverride settings ... its evil but gets the job done + is fast.
John Nicholas
Can you direct me to the "forceOverride settings" that could be useful.
rpetrich
+8  A: 

Perhaps you should consider using some sort of pooling architecture, where you preallocate a number of items up front then lease them from the pool. This keeps the memory requirements nicely pinned. There are a few implementations on MSDN that might serve as reference:

http://msdn2.microsoft.com/en-us/library/bb517542.aspx

http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.socketasynceventargs.aspx

...or I can offer my generic implementation if required.

spender
however you are probably not using structs at the moment because you needed inheritance ... if that is the case this model could be very hard to implement (from my experience - hence finding that forceLayout setting runtime.interop iirc)
John Nicholas
Pooling is a great pattern to choose, but often the pool itself isn't needed: just allocate one object and reuse it (example: Any of the System.Security.Cryptography.HashAlgorithm subclasses)
rpetrich
+1  A: 

The only time items are collected in garbage collection is when there are no more references to the object.

You should make a static class or something to keep a lifetime reference to the object for the life of the application.

If you want to manage your own memory it is possible using unsafe in C#, but you would be better to choose a language that wasn't managed like C++.

David Basarab
+3  A: 

The obvious option is using Marshal.AllocHGlobal and Marshal.FreeHGlobal. I also have a copy of the DougLeaAllocator (dlmalloc) written and battle-tested in C#. If you want, I can get that out to you. Either way will require careful, consistent usage of IDisposable.

280Z28
DougLeaAllocator sounds interesting. I'd like a copy. Is there a place on the web to download/read about the C# version? My email is mark dot kharitonov on gmail. Thanks.
mark
A: 

Although I don't have experience with it, you can try to write C# unmanaged code. Or maybe, you can tell GC to not collect your objects by calling

GC.KeepAlive(obj);
Luis Filipe