Solved

Memory error

Posted on 2007-11-16
15
197 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
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 new code templates in NetBeans IDE 8.0 for Windows.

809 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