Solved

Memory error

Posted on 2007-11-16
15
195 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now