Red Gate forums :: View topic - Pinned arrays or native arrays?
Return to RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net forum

Pinned arrays or native arrays?

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message

Joined: 20 Dec 2008
Posts: 2
Location: Israel

PostPosted: Sat Dec 20, 2008 11:02 pm    Post subject: Pinned arrays or native arrays? Reply with quote


I'm writing a C# wrapper for a native imaging library.

I can allocate the image data on the managed heap, pin it and pass it to the native structure as a pointer, or allocate it on the native heap.

What's better in terms of time performance?

Thanks in advance,
Back to top
View user's profile Send private message

Joined: 30 Oct 2006
Posts: 428
Location: Cambridge, UK

PostPosted: Mon Dec 22, 2008 9:53 am    Post subject: Reply with quote


I guess this is probably one of those "it depends" answers Smile.

Apparently the pinning operation for managed objects is cheap, and I'm guessing that the allocation costs will probably be similar both ways. The major difference is probably going to be in terms of freeing / GC costs.

If you're allocating large objects (>85K I think), they go on to the Large Object Heap (lots more info here), which firstly only gets collected during a Gen 2 GC (the most expensive), and secondly doesn't get compacted, so you might run into virtual memory fragmentation issues, on 32-bit systems at least.

If you allocate natively, you've of course got more control over when you free the memory, and if you're using a lot of these structures, you might get some benefit by not giving the GC so much work to do.

But I suspect the best thing to do is going to be to write it either way, see where your speed issues actually are, and then move on from there - there's no point optimising the memory allocation code if it's only 0.1% of the execution time! Wink.

Hope that helps,
Robert Chipperfield
Developer, Red Gate Software Ltd
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group