为什么在循环中追加到TextBox.Text会在每个迭代中占用更多内存?(Why does appending to TextBox.Text during a loop take up more m

系统教程 行业动态 更新时间:2024-06-14 17:02:17
为什么在循环中追加到TextBox.Text会在每个迭代中占用更多内存?(Why does appending to TextBox.Text during a loop take up more memory with each iteration?)

短问题

我有一个循环运行18万次。 在每次迭代结束时,它应该将结果附加到实时更新的TextBox。

使用MyTextBox.Text += someValue导致应用程序吃了大量的内存,并在几千条记录之后耗尽了可用的内存。

有没有更有效的方式来附加文本到TextBox.Text 18万次?

编辑我真的不关心这个具体情况的结果,但是我想知道为什么这似乎是一个记忆,如果有一个更有效的方式来附加文本到一个TextBox。


长(原)问题

我有一个小应用程序读取CSV文件中的ID号列表,并为每个应用程序生成一个PDF报告。 生成每个pdf文件后, ResultsTextBox.Text将附加已处理并已成功处理的报告的ID号。 该进程在后台线程上运行,因此在项目被处理时,ResultsTextBox会实时更新

我目前正在运行该应用程序的180,000 ID号码,但应用程序正在占用的内存随着时间的推移呈指数级增长。 它开始在大约90K,但是大约3000个记录大约需要250MB,而4000个记录的应用程序占用了大约500MB的内存。

如果我注释到结果文本框的更新,内存保持相对静止在大约90K,所以我可以假设写ResultsText.Text += someValue是什么导致它吃内存。

我的问题是,为什么会这样? 什么是更好的方法来附加一个TextBox.Text数据不吃内存?

我的代码如下所示:

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 }); }

还应该值得一提的是,该应用程序是一次性的,无论如何,需要花费几个小时(或几天))来生成所有报告。 我的主要担心是,如果它达到系统内存限制,它将停止运行。

我很好,留下更新结果TextBox注释出来运行这个东西,但我想知道是否有更多的内存有效的方式来附加一个TextBox.Text为未来的项目。

Short Question

I have a loop that runs 180,000 times. At the end of each iteration it is supposed to append the results to a TextBox, which is updated real-time.

Using MyTextBox.Text += someValue is causing the application to eat huge amounts of memory, and it runs out of available memory after a few thousand records.

Is there a more efficient way of appending text to a TextBox.Text 180,000 times?

Edit I really don't care about the result of this specific case, however I want to know why this seems to be a memory hog, and if there is a more efficient way to append text to a TextBox.


Long (Original) Question

I have a small app which reads a list of ID numbers in a CSV file and generates a PDF report for each one. After each pdf file is generated, the ResultsTextBox.Text gets appended with the ID Number of the report that got processed and that it was successfully processed. The process runs on a background thread, so the ResultsTextBox gets updated real-time as items get processed

I am currently running the app against 180,000 ID numbers, however the memory the application is taking up is growing exponentially as time goes by. It starts by around 90K, but by about 3000 records it is taking up roughly 250MB and by 4000 records the application is taking up about 500 MB of memory.

If I comment out the update to the Results TextBox, the memory stays relatively stationary at roughly 90K, so I can assume that writing ResultsText.Text += someValue is what is causing it to eat memory.

My question is, why is this? What is a better way of appending data to a TextBox.Text that doesn't eat memory?

My code looks like this:

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 worth mentioning that the app is a one-time thing and it doesn't matter that it is going to take a few hours (or days :)) to generate all the reports. My main concern is that if it hits the system memory limit, it will stop running.

I'm fine with leaving the line updating the Results TextBox commented out to run this thing, but I would like to know if there is a more memory efficient way of appending data to a TextBox.Text for future projects.

最满意答案

我怀疑内存使用量如此之大的原因是因为文本框维护堆栈,以便用户可以撤消/重做文本。 在您的情况下似乎不需要该功能,因此请尝试将IsUndoEnabled设置为false。

I suspect the reason the memory usage is so large is because textboxes maintain a stack so that the user can undo/redo text. That feature doesn't seem to be required in your case, so try setting IsUndoEnabled to false.

更多推荐

本文发布于:2023-04-21 18:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/268545e7292f106664bd0e4d38308b92.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   内存   迭代   更多   TextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!