Arrays - memory management

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris  Ashley

    Arrays - memory management

    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
  • Jon Skeet [C# MVP]

    #2
    Re: Arrays - memory management

    On Jun 13, 10:01 am, Chris Ashley <chris.ashl...@ gmail.comwrote:
    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

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: Arrays - memory management

      Chris Ashley wrote:
      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.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Chris  Ashley

        #4
        Re: Arrays - memory management

        On 13 Jun, 13:28, Göran Andersson <gu...@guffa.co mwrote:
        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.

        Comment

        Working...