Solved

Memory error

Posted on 2007-11-16
15
202 Views
Last Modified: 2013-12-17
Hi Experts,

Below is the code. I have 3 tables, the second table contains 100,000 rows ,its afiling at RenderControl with the
execption : Exception of type System.OutOfMemoryException was thrown.
Please advise.
foreach (Table table in tables)
{
StringWriter sw = new StringWriter(output);
HtmlTextWriter htw = new HtmlTextWriter(sw);
table.RenderControl(htw);
}

Open in new window

0
Comment
Question by:Bkuniyil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 16

Expert Comment

by:McExp
ID: 20300184
What actually is the code trying to do?
0
 

Author Comment

by:Bkuniyil
ID: 20300219
Will finally save to a file
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20300252
you have to write directly to file. Associate your writer with a FileStream.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 16

Expert Comment

by:McExp
ID: 20300315
What he said!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20300421
try with this:

TextWriter sw =  new StreamWriter("somefile.html");
HtmlTextWriter htw = new HtmlTextWriter(sw);
 
foreach (Table table in tables)
{
     table.RenderControl(htw);
}
htw.Close();

Open in new window

0
 

Author Comment

by:Bkuniyil
ID: 20301532
The above code still saves only part of the data to the file "somefile.html"
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20301549
what do you mean with "part of the data" ?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20301581
try to use Flush:
TextWriter sw =  new StreamWriter("somefile.html");
HtmlTextWriter htw = new HtmlTextWriter(sw);
 
foreach (Table table in tables)
{
     table.RenderControl(htw);
     htw.Flush();
}
htw.Close();
0
 

Author Comment

by:Bkuniyil
ID: 20350736
I need to write the loop through each of the tables and write it to the StringBuilder object. Is there any way of doing it without writing it to a file?
StringBuilder output = new StringBuilder();
foreach (Table table in tables)
{
StringWriter sw = new StringWriter(output);
HtmlTextWriter htw = new HtmlTextWriter(sw);
table.RenderControl(htw);
}

Open in new window

0
 

Author Comment

by:Bkuniyil
ID: 20352428
Is there a maximum value that a StringBuilder Object can hold, i get an Out of Memory error.Appreciate all help.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20352525
capacity of a StringBuilder class is 4GB.
0
 

Author Comment

by:Bkuniyil
ID: 20352597
The file i am trying to build through the Stringbuilder object is  2MB file, it should work alright?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20352888
Yes, it should, unless something bad in your loop
0
 

Author Comment

by:Bkuniyil
ID: 20355586
The code in the loop is pasted above.Please advise
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 20355637
>> the second table contains 100,000 rows
assuming each rows consumes 2Kb in unicode, we are talking of about 200,000,000 = 200 Mb of text. But during html construction, maybe you will need even more memory, maybe up to 1 GB, this is too much heavy for any standard computer. I think it is no sense to try to do this in memory, you have to render directly to a file stream.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question