Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:07:48+00:00 2026-05-27T23:07:48+00:00

I have a problem that has been bothering me now for a few days.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:07:48+00:00Added an answer on May 27, 2026 at 11:07 pm

    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.

    I disagree with that conclusion. I see several phases:

    1. The byte array exists and is filled with data
    2. You allocate the float array, but it isn’t filled with data yet.
    3. You fill the float array with data
    4. The byte array isn’t referenced anymore, but hasn’t been collected yet
    5. The byte array has been collected.

    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 00s.

    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:

    1. Reuse buffers. The GC is good for small shortlived objects, but very bad for large shortlived objects. This means you should not use functions that allocate and return large arrays. Instead take them as a parameter, so an existing array can be reused.
    2. 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:

      • You can easily write code that only operates on a single channel.
      • Solid 2D arrays are slow to index, so it’s often faster too.
    3. Do you really want to read all data at once? I’d read in chunks of a few kilobytes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been bothering me for sometime now, I have not settled on
I have a problem that has been nagging me for some time now and
I have a problem that has been consuming me for days. I have a
I have problem that seems to be rather trivial but I was not able
Hi I have some problems that has been bothering me for a week. I
I have a problem that has been stumping me for a while, I wrote
I have a problem that has been bugging me all day. In my code
I have a reoccurring DNS problem that has been plaguing our users, occasionally causing
this problem has been bothering me for days and I cannot figure out why
I have a problem that has been effectively reduced to a Travelling Salesman Problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.