Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling ZeroMemory

Calling ZeroMemory

Scheduled Pinned Locked Moved C#
data-structuresquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaleyX
    wrote on last edited by
    #1

    I have a large byte array which I used to intialize by reallocating but the array can get rather large and my app can generate quite a few of them and it was causing a lot of paging so I decided to reuse the arrays instead of reallocating them but I need to reinitialize the array, is the following correct? [System.Runtime.InteropServices.DllImport("kernel32.dll")] unsafe public static extern void ZeroMemory(System.IntPtr ptr ,uint size); internal void Clear() { GCHandle gch = GCHandle.Alloc(_vector,GCHandleType.Pinned); ZeroMemory(gch.AddrOfPinnedObject(),(uint)_vector.Length); gch.Free(); }

    M 1 Reply Last reply
    0
    • P PaleyX

      I have a large byte array which I used to intialize by reallocating but the array can get rather large and my app can generate quite a few of them and it was causing a lot of paging so I decided to reuse the arrays instead of reallocating them but I need to reinitialize the array, is the following correct? [System.Runtime.InteropServices.DllImport("kernel32.dll")] unsafe public static extern void ZeroMemory(System.IntPtr ptr ,uint size); internal void Clear() { GCHandle gch = GCHandle.Alloc(_vector,GCHandleType.Pinned); ZeroMemory(gch.AddrOfPinnedObject(),(uint)_vector.Length); gch.Free(); }

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      In C# I'd just write a for loop zeroing every entry. Calling out to ZeroMemory complicates things. In fact for most purposes I wouldn't bother zeroing the array, I just keep track of how much of it was used. If, for example, you're trying to convert an array of bytes to a string using the System.Text.Encoding classes, you can use the overload of GetString which takes an offset into the array and a length to convert. Stability. What an interesting concept. -- Chris Maunder

      P 1 Reply Last reply
      0
      • M Mike Dimmick

        In C# I'd just write a for loop zeroing every entry. Calling out to ZeroMemory complicates things. In fact for most purposes I wouldn't bother zeroing the array, I just keep track of how much of it was used. If, for example, you're trying to convert an array of bytes to a string using the System.Text.Encoding classes, you can use the overload of GetString which takes an offset into the array and a length to convert. Stability. What an interesting concept. -- Chris Maunder

        P Offline
        P Offline
        PaleyX
        wrote on last edited by
        #3

        The ZeroMemory method appears to be much faster than zeroing the array in a loop - according to the DevPartner profiler - the array can be over 1 million bytes long.

        D 1 Reply Last reply
        0
        • P PaleyX

          The ZeroMemory method appears to be much faster than zeroing the array in a loop - according to the DevPartner profiler - the array can be over 1 million bytes long.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          I've had MUCH bigger. It's much quicker (and much less of a hassle) to just kill the array and reallocate it. Everything will be automatically zero'd out for you. [EDIT] Whoops! Hit Submit on accident. Just zeroing out the memory won't avoid the problem of paging. In order to zero out the array, it's got to be swapped back in, zero'd, then swapped back out if needed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          P 2 Replies Last reply
          0
          • D Dave Kreskowiak

            I've had MUCH bigger. It's much quicker (and much less of a hassle) to just kill the array and reallocate it. Everything will be automatically zero'd out for you. [EDIT] Whoops! Hit Submit on accident. Just zeroing out the memory won't avoid the problem of paging. In order to zero out the array, it's got to be swapped back in, zero'd, then swapped back out if needed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            P Offline
            P Offline
            PaleyX
            wrote on last edited by
            #5

            That's what I thought but it seems to make a big difference recycling the memory like that. The way the app works means 1000s of these arrays could be created and destroyed - recycling it means I only need 3.

            L 1 Reply Last reply
            0
            • P PaleyX

              That's what I thought but it seems to make a big difference recycling the memory like that. The way the app works means 1000s of these arrays could be created and destroyed - recycling it means I only need 3.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Call GC.Collect() after removing any and all references to the array. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                I've had MUCH bigger. It's much quicker (and much less of a hassle) to just kill the array and reallocate it. Everything will be automatically zero'd out for you. [EDIT] Whoops! Hit Submit on accident. Just zeroing out the memory won't avoid the problem of paging. In order to zero out the array, it's got to be swapped back in, zero'd, then swapped back out if needed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                P Offline
                P Offline
                PaleyX
                wrote on last edited by
                #7

                The array should generally be kept in memory I would have thought as it is the focus of the proceesing at the time - everything centres around that array - although it's on a worker thread so I can keep the GUI updated with the progress. I have been messing about with this code for a while now, memory and performance profiling the hell out of it. It's odd what works and what doesnt.

                D 1 Reply Last reply
                0
                • P PaleyX

                  The array should generally be kept in memory I would have thought as it is the focus of the proceesing at the time - everything centres around that array - although it's on a worker thread so I can keep the GUI updated with the progress. I have been messing about with this code for a while now, memory and performance profiling the hell out of it. It's odd what works and what doesnt.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  I need to keep only 1 "in memory", but I had to swap it every now and then between a working array, which grows in size unpredictably, and a source data array, with a static size. Actually, I was using BitArray's. All I did to conserve and compact memory during the swap was to serialize the current working array to disk (believe me, an array at nearly the capacity of a BitArray object!), dump both arrays from memory, force a Garbage Collect, then recreate the source data array from the serialized object on disk, then blow away the file. Worked great and enabled me to eek out a bit more capacity in the process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  P 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    I need to keep only 1 "in memory", but I had to swap it every now and then between a working array, which grows in size unpredictably, and a source data array, with a static size. Actually, I was using BitArray's. All I did to conserve and compact memory during the swap was to serialize the current working array to disk (believe me, an array at nearly the capacity of a BitArray object!), dump both arrays from memory, force a Garbage Collect, then recreate the source data array from the serialized object on disk, then blow away the file. Worked great and enabled me to eek out a bit more capacity in the process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    P Offline
                    P Offline
                    PaleyX
                    wrote on last edited by
                    #9

                    I am using the byte array as a bit array and I need to constanty iterate over it ANDing and ORing it against another byte array. I am creating a 3 way crosstab report and by recycling the memory I only have 3 byte arrays in memory at any one time which give me big savings. The array is basically the number of "person" records on a database divided by 8

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups