Short question
I have a loop that runs 180,000 times. At the end of each iteration, it is supposed to add the results to the TextBox, which is updated in real time.
Using MyTextBox.Text += someValue leads to the fact that the application consumes huge amounts of memory, and after several thousand entries, it runs out of available memory.
Is there a more efficient way to add text to TextBox.Text 180,000 times?
Edit I really don't care about the result of this particular case, however I want to know why it looks like a bot memory, and if there is a more efficient way to add text to a text field.
Long-running (original) question
I have a small application that reads a list of ID numbers in a CSV file and generates a PDF report for each of them. After creating each PDF file, ResultsTextBox.Text added with the identification number of the report that was processed and that it was successfully processed. The process runs in the background thread, so the ResultsTextBox is updated in real time when the items are processed
I am currently running the application against 180,000 identification numbers, however, the memory that the application occupies grows exponentially with time. It starts at about 90 thousand, but at about 3000 records it takes about 250 MB, and for 4000 records the application takes about 500 MB of memory.
If I comment out the update for the TextBox of the results, the memory remains relatively fixed at about 90K, so I can assume that the entry ResultsText.Text += someValue is what makes it have memory.
My question is: why is this? What is the best way to add data to a TextBox.Text that does not eat memory?
My code is as follows:
try { report.SetParameterValue("Id", id); report.ExportToDisk(ExportFormatType.PortableDocFormat, string.Format(@"{0}\{1}.pdf", new object[] { outputLocation, id})); // ResultsText.Text += string.Format("Exported {0}\r\n", id); } catch (Exception ex) { ErrorsText.Text += string.Format("Failed to export {0}: {1}\r\n", new object[] { id, ex.Message }); }
It should also be noted that the application is one-time, and it does not matter that it takes several hours (or days :) to create all the reports. My main problem is that if it reaches the limit of system memory, it will stop working.
I'm fine, leaving a line updating the TextBox results commented out to run this thing, but I would like to know if there is a more efficient way to store data in TextBox.Text for future projects.