473,657 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(FileS tream 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.Begi nRead(inBuffer, 0,
inBuffer.Length , null, null);
ar.AsyncWaitHan dle.WaitOne();

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

for (i = 0; i < scaledChunk.Len gth; i++)
{
currentShort = (short)((inBuff er[2 * i] << 8) +
inBuffer[2 * i + 1]);
scaledChunk[i] = (scaling[channel, 2] *
(double)current Short
+ scaling[channel, 1]) * scaling[channel, 3];
}

return scaledChunk;
}

Jun 7 '07 #1
4 3138
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

Jun 7 '07 #2
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?

Jun 7 '07 #3
On Jun 7, 3:54 pm, kreut...@gmail. com wrote:
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?
Call EndRead in a call back method.

public static void ReadFileCallbac k(IAsyncResult asyncResult)
{

//Get the state using asyncResult.Asy ncState and get the stream from
state.
//Then call stream.EndRead( asyncResult);. This woud return the size of
data read.
}

Refere Async IO programming in MSDN:http://msdn2.microsoft.com/en-us/
library/aa719596(VS.71) .aspx

Jun 7 '07 #4
On Jun 7, 11:54 am, kreut...@gmail. com wrote:
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?
No - because you're already multi-threaded by the fact that the method
is going to be called many times by many separate threads! By blocking
until the read has finished, you've effectively got much more
complicated code but synchronous IO (per thread).
where would i call EndRead(), in the BeginRead callback delegate?
That's a fairly common place to do it, yes.

Jon

Jun 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2949
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting exactly what I want. Here are my goals 1. I would like the IAsyncResult that is returned by the Begin function to be able to be waited on or polled to check for completion. 2. The Begin function would take a callback, and the async process would
3
2216
by: frustrated | last post by:
I am trying to share a file stream between two threads, but havent got a clue as to how to do it. The first thread will be reading the file, and the second thread will(/might) be writing to the same file stream. I was going to pass a ref to the file stream to the second thread, but the problem comes when I am trying to read from one section of the stream and write to another. I dont want one thread changing the position of the stream...
11
6954
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then making the main thread sleep until the counter gets back to zero. The call back function for each call decrements the counter. Is there a better way to make the thread wait until all calls are complete besides using the counter? I've seen some things...
7
2851
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
2
2318
by: Niels | last post by:
Hi, We have an ASP.NET application that uses Server.CreateObject in the .aspx pages to create (ASP) COM-components (we use the 'aspcompat="true"' option). From the beginning we have seen COM+ errors in the event log and users being hung or getting Internal Server Errors. The error is: The run-time environment has detected an inconsistency in its internal
4
1587
by: amit | last post by:
Hi guys!I am trying to write a program which will segregate some selected keywords from a given file.The source code is given alongwith #include<stdio.h> #include<string.h> char key_set={"ami\0","inc\0","lud\0"}; #define MAX_KW_SIZE 10 #define MAX_OCUR 100
0
1647
by: yogesh_anand | last post by:
Hi I am using sun solaris 5.9 operating system.Sometimes after reading from and while writing to file process size used to get increase.(it can be after 20th times 40 th time) I doesn't happen always but some time.After putting print statements i identified the point.Can any body tell me whether it will going to come in memory leak or normal process usuage.If it comes under memory leak how to slove?I have marked the code below from where it...
15
8397
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast (asynchronously). Then it repackages these UDP packets and sends them to a subscriber via TCP. Now, I can read the UDP stream all day long without the application
39
2842
by: cj | last post by:
I have a 2005 TCP/IP server that creates a new thread to handle each incoming TCP/IP request. Once the request has been answered by the thread the TCP/IP socket is disconnected and the sub/thread ends. This program originally written in 2003 and I rewrote it in 2005 earlier this year. I don't know how long this has been going on but we just noticed today that the program was using over a gig of virtual memory. Real memory was quite...
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.