1

I am converting a HTML string to PDF by using iTextSharp XMLWorker. previuosly this was working fine and generates nice pdf files but later I added base64 string Images to My HTML string which I want to show in my PDF. it is working fine for few images( small string ) but when number of images increases parallelly the size of the string increases. I am returning HTML string from a function like this

sb = new StringBuilder();
tw = new StringWriter(sb);
hw = new HtmlTextWriter(tw);
tblDistrictwiseResults.RenderControl(hw);
return sb.ToString();

and now returned HTML string is appending to another string like below

StringBuilder sb = new StringBuilder();

sbFooter = new StringBuilder();
tw = new StringWriter(sbFooter);
hw = new HtmlTextWriter(tw);
tblFooter.RenderControl(hw);


string pdfString = GetPDFString(paperCode, paperName, true);  //This is the string returned from anothe function
sb.Append(pdfString + "<br />" + sbFooter.ToString()); //concatinating returned string and footer string and saving in another stringbuilder( here I am gettiong OOM Exception )

and the string becomes very large but it did not exceed maxcapacity of the StringBuilder.

Please someone help me

EDIT: I am using VS2010(32 bit) and DEV Server 32 bit

13
  • 2
    "previuosly this was working fine and generates nice pdf files but later I added base64 string Images to My HTML string which I want to show in my PDF." It does not realy mater what your memory is. Images that you then Base64 encode are going to break it. | You should try not to make the whole document in one go. Maybe chapter by chapter? Commented May 30, 2020 at 1:10
  • 5
    Does this answer your question? C# Stringbuilder OutOfMemoryException Commented May 30, 2020 at 1:18
  • 2
    Note that when you concatenate all three strings together with "+" before calling sb.Append, that you are briefly consuming the entire space for the three strings together in a single block of memory, then calling sb.Append which will add another equal-sized block of memory (on average) to the StringBuffer while still holding a reference to the first via its parameter, and then only after the Append method returns will the first block be released for GC. It would probably be better to have three Append calls for each of the pieces instead of concatenating them before calling Append. Commented May 30, 2020 at 1:41
  • 1
    Try to make your 32 bit app LARGEADDRESSAWARE. Could solve the problem. See How to make a .NET application large address aware? to know how. Also, consider the first comment here. Commented May 30, 2020 at 1:42
  • 1
    My machine is 64 bit makes absolutely no difference if your code is 32-bit. 32-bit code is limited to 2 GB of total memory unless you've enabled LARGEADDRESSAWARE as @JQSOFT mentioned (which IIRC extends it to 3 GB). Your machine could be 64-bit with 12 TB of RAM and it wouldn't change the limits allowed by 32-bit code. Commented May 30, 2020 at 2:18

1 Answer 1

0

.NET has memory limits depending on what binarity you run in. And ones that are leagues short of the OS limits, neveremind the hardware limits: https://learn.microsoft.com/en-us/windows/win32/memory/memory-limits-for-windows-releases

Plus the 2 GiB object limit. And strings or arrays (wich I am betting string builder uses) run into the object size or memory limits notoriously quickly.

By default all .NET code is bitness agnostic. The same code can run as x32 or x64 - or even x16 or x128, if we ever get a runtime for that. Fixing the process to only run on x64 binarity can fix the immediate problem. However the bigger problem is the process itself:

Strings are easily capable of breaking the Memory limits. But they got nothing on Images. Images are huge! They are the book example for a BLOB - Binary Large OBject - a scary thing to keep in memory.

Base64 encoding them makes them about 33% bigger. If you then try to keep a whole PDF document of such giant strings in memory, you can only run into issues. It is a wonder it worked thus far. You need to change your process, so you get way smaler strings in memory. Wich may actually mean a whole lot less concatenation, wich may even invalidate having to use StringBuilder in the first place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.