Harsha
Harsha

Reputation: 1879

Clearing Byte Array

We are currently using .net DLLs which are made from MATLAB and We are sending byte Array from C# to Method in this DLL. Each time we call, we are creating byte array of 6MB. Is there is any way to clear this array when the function returns?

We already tested with GC.Collect() but no luck.

Thanks in Advance.

Upvotes: 0

Views: 8593

Answers (3)

andy
andy

Reputation: 6079

First you clear your array by using Array.Clear and then de-reference the array using yourArray = null, and the re-declare it

Upvotes: 1

Kjartan
Kjartan

Reputation: 19081

Clearing out the array can be done with Array.Clear, but that will not release memory as long as there is a reference to the array; since an array has a constant size, it's content is not really relevant.

What you need to do is ensure there are no references to it left. Only after that will the garbage collector handle it and free the memory (although there is no guarantee as to exactly when that will happen).

This can happen automatically when the relevant array variable is no longer in scope, and there is nothing else referring to it. If you need to "remove" it manually, you can achieve this by setting the variable to null. That is probably (?) the quickest way to enable the GC to discover that there is something to be collected here...

Upvotes: 2

Ehsan
Ehsan

Reputation: 32651

you can use Array.Clear to clear out the array. For example

Array.Clear(YourByteArray,0,YourByteArray.Length);

Upvotes: 5

Related Questions