473,763 Members | 7,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Net.Scat terGatherBuffer s.MemoryChuck allocates large byte[]

Summary: System.Net.Scat terGatherBuffer s.MemoryChuck allocates inordinately
large byte[]s when sending large post data.

The following application consumes inordinate quantities of memory. My code
does not explicitly allocate memory in a loop nor does it explicitly allocate
large blocks of memory. Yet, the application’s memory footprint will grow as
large as 370 MB. Rarely will it run to completion; usually, it throws an out
of memory exception or locks up just before sending the packet that would
total 512 MBs.

SciTech .NET Memory Profiler, reports that
System.Net.Scat terGatherBuffer s.MemoryChuck is holding onto very large blocks
of memory, specifically byte[]s, which it appears to have allocated in a
doubling fashion. For example, the sizes of these blocks are 226MB, 113MB,
56.5MB, 28MB, 14MB, and so on down. It’s as if
System.Net.Scat terGatherBuffer s is allocating a byte of memory for every byte
I send on the socket, but is allocating increasingly large buffers to store
this.

If I send a smaller amount of data, the send completes, but the
System.Net.Scat terGatherBuffer s.MemoryChuck byte[]s are not released when the
socket stream is closed, all references to the WebRequest and its stream are
released, and a garbage collection is explicitly requested.

I’ve also tried performing the socket communication on the UI message pump
thread without seeing a different behavior.

I’m running Windows 2000 on a box with 512 MB of memory. I ran this test on
..NET Fx 1.1 SP1. I haven’t tried any other framework versions.

Am I doing something wrong? Is this a known issue? Do you have any
suggestions or explanations?

Thanks in advance for any help you can provide.
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threadin g;

namespace SocketMemoryLea k {
public class Form1 : System.Windows. Forms.Form {
private const int KB = 1024;
private const int MB = KB * KB;
private System.Windows. Forms.Button btnSend;
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.Label lblMBsSent;
private System.Windows. Forms.Label lblGCed;
private System.Windows. Forms.Label lblMemoryAlloca ted;

public Form1() {
InitializeCompo nent();
}

protected override void Dispose(bool disposing) {
if (disposing) {
if (components != null) {
components.Disp ose();
}
}
base.Dispose(di sposing);
}

#region Windows Form Designer generated code
private void InitializeCompo nent() {
this.btnSend = new System.Windows. Forms.Button();
this.lblMBsSent = new System.Windows. Forms.Label();
this.lblGCed = new System.Windows. Forms.Label();
this.lblMemoryA llocated = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// btnSend
//
this.btnSend.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.btnSend.Lo cation = new System.Drawing. Point(16, 8);
this.btnSend.Na me = "btnSend";
this.btnSend.Si ze = new System.Drawing. Size(184, 23);
this.btnSend.Ta bIndex = 0;
this.btnSend.Te xt = "Start";
this.btnSend.Cl ick += new System.EventHan dler(this.btnSe nd_Click);
//
// lblMBsSent
//
this.lblMBsSent .Anchor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.lblMBsSent .Location = new System.Drawing. Point(16, 40);
this.lblMBsSent .Name = "lblMBsSent ";
this.lblMBsSent .Size = new System.Drawing. Size(184, 23);
this.lblMBsSent .TabIndex = 1;
this.lblMBsSent .Text = "Sent: 0 MBs";
this.lblMBsSent .TextAlign =
System.Drawing. ContentAlignmen t.MiddleLeft;
//
// lblGCed
//
this.lblGCed.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.lblGCed.Lo cation = new System.Drawing. Point(16, 88);
this.lblGCed.Na me = "lblGCed";
this.lblGCed.Si ze = new System.Drawing. Size(184, 23);
this.lblGCed.Ta bIndex = 2;
this.lblGCed.Te xt = "No previous garbage collection";
this.lblGCed.Te xtAlign =
System.Drawing. ContentAlignmen t.MiddleLeft;
//
// lblMemoryAlloca ted
//
this.lblMemoryA llocated.Anchor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.lblMemoryA llocated.Locati on = new System.Drawing. Point(16,
64);
this.lblMemoryA llocated.Name = "lblMemoryAlloc ated";
this.lblMemoryA llocated.Size = new System.Drawing. Size(184, 23);
this.lblMemoryA llocated.TabInd ex = 3;
this.lblMemoryA llocated.Text = "Memory Allocated: 0 MB";
this.lblMemoryA llocated.TextAl ign =
System.Drawing. ContentAlignmen t.MiddleLeft;
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(208, 117);
this.Controls.A dd(this.lblMemo ryAllocated);
this.Controls.A dd(this.lblGCed );
this.Controls.A dd(this.lblMBsS ent);
this.Controls.A dd(this.btnSend );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

[STAThread]
static void Main() {
Application.Run (new Form1());
}

private void btnSend_Click(o bject sender, System.EventArg s e) {
this.btnSend.En abled = false;
Thread t = new Thread(new ThreadStart(thi s.Send));
t.IsBackground = true;
t.Start();
}

private void DoGC(int bytesSent) {
this.lblGCed.Te xt = "Starting GC...";
GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();
this.lblGCed.Te xt = "GC'd at " + (bytesSent / MB) + " MB";
}

private void SetSent(int bytesSent) {
this.lblMBsSent .Text = "Sent: " + (bytesSent / MB) + " MB";
this.lblMemoryA llocated.Text = "Memory Allocated: " +
(Environment.Wo rkingSet / MB) + " MB";
}

private void Send() {
byte[] buffer = new byte[16 * KB];
int totalBytesToSen d = 514 * MB;

HttpWebRequest request = (HttpWebRequest )
WebRequest.Crea te("http://marias/cgi-bin/leif.cfg/php/enduser/fileUpload.php" );
request.Method = "POST";
request.Content Length = totalBytesToSen d;
Stream requestStream = request.GetRequ estStream();

int bytesSent = 0;
int gcInterval = 64 * MB;
int displayInterval = 1 * MB;
while (bytesSent < totalBytesToSen d) {
requestStream.W rite(buffer, 0, buffer.Length);
bytesSent += buffer.Length;
if (bytesSent % displayInterval == 0) {
this.SetSent(by tesSent);
}
if (bytesSent % gcInterval == 0) {
this.DoGC(bytes Sent);
}
}

requestStream.F lush();
requestStream.C lose();
requestStream = null;
request = null;

this.DoGC(bytes Sent);
MessageBox.Show ("Send thread ending.");
this.btnSend.En abled = true;
}
}
}
Nov 16 '05 #1
1 4012
this is because httpwebrequest buffers data, as it migt need to repost
it in so9me cases.

you can disable this by setting AllowWriteStrea mBuffering=fals e

-----
feroze

This posting is provided as-is

http://weblogs.asp.net/feroze_daud
lwickland <lw*******@disc ussions.microso ft.com> wrote in message news:<F5******* *************** ************@mi crosoft.com>...
Summary: System.Net.Scat terGatherBuffer s.MemoryChuck allocates inordinately
large byte[]s when sending large post data.

The following application consumes inordinate quantities of memory. My code
does not explicitly allocate memory in a loop nor does it explicitly allocate
large blocks of memory. Yet, the application’s memory footprint will grow as
large as 370 MB. Rarely will it run to completion; usually, it throws an out
of memory exception or locks up just before sending the packet that would
total 512 MBs.

SciTech .NET Memory Profiler, reports that
System.Net.Scat terGatherBuffer s.MemoryChuck is holding onto very large blocks
of memory, specifically byte[]s, which it appears to have allocated in a
doubling fashion. For example, the sizes of these blocks are 226MB, 113MB,
56.5MB, 28MB, 14MB, and so on down. It’s as if
System.Net.Scat terGatherBuffer s is allocating a byte of memory for every byte
I send on the socket, but is allocating increasingly large buffers to store
this.

If I send a smaller amount of data, the send completes, but the
System.Net.Scat terGatherBuffer s.MemoryChuck byte[]s are not released when the
socket stream is closed, all references to the WebRequest and its stream are
released, and a garbage collection is explicitly requested.

I’ve also tried performing the socket communication on the UI message pump
thread without seeing a different behavior.

I’m running Windows 2000 on a box with 512 MB of memory. I ran this test on
.NET Fx 1.1 SP1. I haven’t tried any other framework versions.

Am I doing something wrong? Is this a known issue? Do you have any
suggestions or explanations?

Thanks in advance for any help you can provide.
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threadin g;

namespace SocketMemoryLea k {
public class Form1 : System.Windows. Forms.Form {
private const int KB = 1024;
private const int MB = KB * KB;
private System.Windows. Forms.Button btnSend;
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.Label lblMBsSent;
private System.Windows. Forms.Label lblGCed;
private System.Windows. Forms.Label lblMemoryAlloca ted;

public Form1() {
InitializeCompo nent();
}

protected override void Dispose(bool disposing) {
if (disposing) {
if (components != null) {
components.Disp ose();
}
}
base.Dispose(di sposing);
}

#region Windows Form Designer generated code
private void InitializeCompo nent() {
this.btnSend = new System.Windows. Forms.Button();
this.lblMBsSent = new System.Windows. Forms.Label();
this.lblGCed = new System.Windows. Forms.Label();
this.lblMemoryA llocated = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// btnSend
//
this.btnSend.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.btnSend.Lo cation = new System.Drawing. Point(16, 8);
this.btnSend.Na me = "btnSend";
this.btnSend.Si ze = new System.Drawing. Size(184, 23);
this.btnSend.Ta bIndex = 0;
this.btnSend.Te xt = "Start";
this.btnSend.Cl ick += new System.EventHan dler(this.btnSe nd_Click);
//
// lblMBsSent
//
this.lblMBsSent .Anchor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.lblMBsSent .Location = new System.Drawing. Point(16, 40);
this.lblMBsSent .Name = "lblMBsSent ";
this.lblMBsSent .Size = new System.Drawing. Size(184, 23);
this.lblMBsSent .TabIndex = 1;
this.lblMBsSent .Text = "Sent: 0 MBs";
this.lblMBsSent .TextAlign =
System.Drawing. ContentAlignmen t.MiddleLeft;
//
// lblGCed
//
this.lblGCed.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.lblGCed.Lo cation = new System.Drawing. Point(16, 88);
this.lblGCed.Na me = "lblGCed";
this.lblGCed.Si ze = new System.Drawing. Size(184, 23);
this.lblGCed.Ta bIndex = 2;
this.lblGCed.Te xt = "No previous garbage collection";
this.lblGCed.Te xtAlign =
System.Drawing. ContentAlignmen t.MiddleLeft;
//
// lblMemoryAlloca ted
//
this.lblMemoryA llocated.Anchor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.lblMemoryA llocated.Locati on = new System.Drawing. Point(16,
64);
this.lblMemoryA llocated.Name = "lblMemoryAlloc ated";
this.lblMemoryA llocated.Size = new System.Drawing. Size(184, 23);
this.lblMemoryA llocated.TabInd ex = 3;
this.lblMemoryA llocated.Text = "Memory Allocated: 0 MB";
this.lblMemoryA llocated.TextAl ign =
System.Drawing. ContentAlignmen t.MiddleLeft;
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(208, 117);
this.Controls.A dd(this.lblMemo ryAllocated);
this.Controls.A dd(this.lblGCed );
this.Controls.A dd(this.lblMBsS ent);
this.Controls.A dd(this.btnSend );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

[STAThread]
static void Main() {
Application.Run (new Form1());
}

private void btnSend_Click(o bject sender, System.EventArg s e) {
this.btnSend.En abled = false;
Thread t = new Thread(new ThreadStart(thi s.Send));
t.IsBackground = true;
t.Start();
}

private void DoGC(int bytesSent) {
this.lblGCed.Te xt = "Starting GC...";
GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();
this.lblGCed.Te xt = "GC'd at " + (bytesSent / MB) + " MB";
}

private void SetSent(int bytesSent) {
this.lblMBsSent .Text = "Sent: " + (bytesSent / MB) + " MB";
this.lblMemoryA llocated.Text = "Memory Allocated: " +
(Environment.Wo rkingSet / MB) + " MB";
}

private void Send() {
byte[] buffer = new byte[16 * KB];
int totalBytesToSen d = 514 * MB;

HttpWebRequest request = (HttpWebRequest )
WebRequest.Crea te("http://marias/cgi-bin/leif.cfg/php/enduser/fileUpload.php" );
request.Method = "POST";
request.Content Length = totalBytesToSen d;
Stream requestStream = request.GetRequ estStream();

int bytesSent = 0;
int gcInterval = 64 * MB;
int displayInterval = 1 * MB;
while (bytesSent < totalBytesToSen d) {
requestStream.W rite(buffer, 0, buffer.Length);
bytesSent += buffer.Length;
if (bytesSent % displayInterval == 0) {
this.SetSent(by tesSent);
}
if (bytesSent % gcInterval == 0) {
this.DoGC(bytes Sent);
}
}

requestStream.F lush();
requestStream.C lose();
requestStream = null;
request = null;

this.DoGC(bytes Sent);
MessageBox.Show ("Send thread ending.");
this.btnSend.En abled = true;
}
}
}

Nov 16 '05 #2

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

Similar topics

0
913
by: zhimin | last post by:
Hi, I'm writing a program to send large file(100m) through dotnet using TCPListener & TCPClient, I'm sending the file with a ask and response loop: 1. Client send a flag 1 to server indicate it has data send to server. 2. Client send the buffer block size. 3. Client send the actual buffer to the server. 4. Server send a flag 1 to client indicating that the buffer has been successfully receeived. 5. The next loop until all data of the...
12
8127
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a second phase I search for the address that starts on a 16 byte boundary. And I then use that new...
0
5672
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at TryThis.Form1.save() in C:\Documents and Settings\Nick\My Documents\...
4
47727
by: Daniel | last post by:
Exception of type System.OutOfMemoryException was thrown. this error is occuring when on arbitrary threads in my .net windows service. There is 2 gigs of memory on the machine and this .net windows service only uses 50 megabytes. What could be causing this? how to fix?
14
2865
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding functions, I would greatly appreciate the help. Thanks, Ché #include <iostream>
7
4190
by: StupidScript | last post by:
>From the manual "Storage Requirements": "ENUM('value1','value2',...) =1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)" This seems to mean: "a" = 1 byte of storage "prestidigitation" = 1 byte of storage
4
6985
by: Jonathan Wood | last post by:
Does anyone know why the documentation for System.Security.Cryptography.MD5.Create() seems to omit completely any description of allowed arguments. I'm trying to convert some C++ code to C# and seem to be getting different MD5 results. If other algorithms are available, maybe that is related to the problem I'm having. But the docs for this method don't tell me anything about what algorithms are supported? Thanks.
30
3982
by: nano2k | last post by:
Hi I need an efficient method to convert a string object to it's byte equivalent. I know there are LOTS of methods, but they lack in efficiency. All methods allocate new memory to create the byte array. Of course, when memory allocation occurs, then naturally extra processing power is needed. To more explicit, MFC introduced a super-efficient method of dealing with this situation. As far as I remember (I switched from MFC to .NET few...
2
3632
by: Aryan | last post by:
Hi, I am using C# with framework 2.0 and creating PDF files on-fly, along with this I am using Windows 2003 Server. I am using Byte to take the data input and then save into pdf format on harddrive location. Now after creating few successful pdf files, I am getting "System.OutOfMemoryException".
0
9563
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
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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
9937
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
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
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
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.