Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Async file read causes memory leak


hey,

can anyone explain to me why this method that is called numerous times
to read chunks of a file works fine with a normal synchronous read,
but causes a large memory leak when BeginRead() is called instead?

        private double[] ReadChunk(FileStream fileReader, int
readLength, int channel)
        {
            short currentShort;
            int i;
            byte[] inBuffer = new byte[2 * readLength];
            double[] scaledChunk = new double[readLength];

            // causes massive memory leak, why?
            IAsyncResult ar = fileReader.BeginRead(inBuffer, 0,
inBuffer.Length, null, null);
            ar.AsyncWaitHandle.WaitOne();

            // works fine if i just call this
            //fileReader.Read(inBuffer, 0, inBuffer.Length);

            for (i = 0; i < scaledChunk.Length; i++)
            {
                currentShort = (short)((inBuffer[2 * i] << 8) +
inBuffer[2 * i + 1]);
                scaledChunk[i] = (scaling[channel, 2] *
(double)currentShort
                    + scaling[channel, 1]) * scaling[channel, 3];
            }

            return scaledChunk;
        }

On Jun 7, 10:56 am, kreut@gmail.com wrote:

> can anyone explain to me why this method that is called numerous times
> to read chunks of a file works fine with a normal synchronous read,
> but causes a large memory leak when BeginRead() is called instead?

Are you ever calling EndRead? If not, that's probably the problem.

(I assume that in your real code there's a point to using asynchronous
IO. If you're just going to wait for it to complete, you might as well
do it synchronously to start with.)

Jon

-----------------------------------------------Reply-----------------------------------------------

On 7 Jun, 11:39, "Jon Skeet [C# MVP]" <s@pobox.com> wrote:

> On Jun 7, 10:56 am, kreut@gmail.com wrote:

> > can anyone explain to me why this method that is called numerous times
> > to read chunks of a file works fine with a normal synchronous read,
> > but causes a large memory leak when BeginRead() is called instead?

> Are you ever calling EndRead? If not, that's probably the problem.

> (I assume that in your real code there's a point to using asynchronous
> IO. If you're just going to wait for it to complete, you might as well
> do it synchronously to start with.)

> Jon

hey jon

yes, there is a point (i think) in reading asynchronously, in that
this method is going to be called many times by separate threads
running at the same time. am i correct in thinking that using async
reads will improve performance?

where would i call EndRead(), in the BeginRead callback delegate?

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc