Solved

Memory error

Posted on 2007-11-16
15
203 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
SharePoint Admin?

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

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

617 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