Calling ZeroMemory
-
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(); }
-
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(); }
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 ofGetString
which takes an offset into the array and a length to convert. Stability. What an interesting concept. -- Chris Maunder -
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 ofGetString
which takes an offset into the array and a length to convert. Stability. What an interesting concept. -- Chris Maunder -
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.
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
-
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
-
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.
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
-
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
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.
-
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.
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
-
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
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