Arrays - memory management

C

Chris Ashley

Probably some very basic questions...

I understand system.array is a reference type, is this the case if the
array is made up of value types? Or are they stored differently on the
managed heap?

Is there any way to explicitly deallocate memory for an array of value
types, or do I have to wait for a garbage collection cycle?

I am working with byte arrays, would there be any speed advantage to
using AllocHGlobal and using unsafe pointers as opposed to byte
arrays?

Thanks,

Chris
 
J

Jon Skeet [C# MVP]

Probably some very basic questions...

I understand system.array is a reference type, is this the case if the
array is made up of value types?
Yes.

Or are they stored differently on the managed heap?

No. However, any "large" objects (over a certain limit - it used to be
80K but it may have been changed) go on a separate part of the heap.

Normally those "large" obejcts are only arrays or strings.
Is there any way to explicitly deallocate memory for an array of value
types, or do I have to wait for a garbage collection cycle?

Just like with everything else, you need to wait for the garbage
collector. (Or call GC.Collect() to encourage it to run.)
I am working with byte arrays, would there be any speed advantage to
using AllocHGlobal and using unsafe pointers as opposed to byte
arrays?

Unlikely. I'd certainly advise against it from a code simplicity point
of view, unless you found you had significant issues.

Jon
 
G

Göran Andersson

Chris said:
Probably some very basic questions...

I understand system.array is a reference type, is this the case if the
array is made up of value types? Or are they stored differently on the
managed heap?

The array itself is always an object in the heap. For an array of value
types, the array contains the values. For an array of reference types,
the array contains references.
Is there any way to explicitly deallocate memory for an array of value
types, or do I have to wait for a garbage collection cycle?

No, as the array contains the values, the values can not be collected
separate from the array itself.
I am working with byte arrays, would there be any speed advantage to
using AllocHGlobal and using unsafe pointers as opposed to byte
arrays?

Likely the opposite. Allocating managed memory is a simpler process than
allocating unmanaged memory.
 
C

Chris Ashley

Likely the opposite. Allocating managed memory is a simpler process than
allocating unmanaged memory.

Hi Goran,

Thanks for your reply. I am working with multiple large byte arrays
(~20mb) in a server application so am concerned about the memory used
by them not being freed efficiently since I understand collection
cycles on the large object heap are very infrequent. Is this true? And
if so, I imagine that any speed overhead from using AllocHGlobal/
FreeHGlobal would be outweighed by the benefits of more efficient
memory management? Would appreciate thoughts on this.
 

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