I have a problem that has been bothering me now for a few days. I have tried to Google the problem, but so far have not been able to find any solutions, not even a single person with the same problem.
It seems that the C# method System.Buffer.BlockCopy leaves you with some sort of memory ghosts. I have for example this method:
private float[,] readFloatArray2 (byte[] b) {
int floatSize = sizeof(float);
float[,] v = new float[2, (b.Length / 2) / floatSize];
System.Buffer.BlockCopy(b, 0, v, 0, b.Length);
return v;
}
to convert a byte array to a 2D float array. The data is previously read from a stream. I have located the problem to be the System.Buffer.BlockCopy method.
If I remove the BlockCopy command, the memory used by the application will be half the size. this means that its not my fault that the byte array is still alive. because without the BlockCopy command, the byte array dies properly. the float array gets created anyway (with or without the copied information).
I am not quite sure if this is a problem of the BlockCopy command or the GC because I have also tried to call System.GC.Collect(); after the BlockCopy and then it also works perfectly (I know you should not do this... thats why I am asking for advise here).
I also wouldn't bother asking, but the issue involves several hundred meg.
Besides the memory issues, the method works perfectly fine. Does anyone know what causes the memory problem?
greetings and thanks in advance oli
ps: I am using .NET4.0 with Visual Studio 2010 PRO and WIN7... don't know whether this is relevant or not.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.
The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
I have located the problem to be the
System.Buffer.BlockCopy
method. If I remove theBlockCopy
command, the memory used by the application will be half the size. this means that its not my fault that the byte array is still alive. Because without theBlockCopy
command, the byte array dies properly.
I disagree with that conclusion. I see several phases:
The live of the byte array isn't influenced by BlockCopy
.
Step 2 reserves and commits virtual memory. So the commit size grows in this step. But since the content of the array has never been written to, and consists entirely of 00
bytes, the windows memory manager doesn't allocate any physical memory for it. It just notes that these pages consist entirely of 00
s.
The physical memory for the float array get only allocated in step 3. You'd get the same effect if you'd add a loop that initialized every field in the array.
Apart from the actual problem, I also have some design suggestions:
If you're using working with audio data(Seems likely), I wouldn't use a solid 2D array. I'd instead use an array of arrays. Where the inner array represents the samples in a single buffer, and the outer array represents the buffers. This has two advantages:
BlockCopy
doesn't have managed .NET implementation. Internally, it invokes external win api.
[SecuritySafeCritical]
public static extern void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With