views:

276

answers:

2

I am working on a program that reads in from a serial port, then parses, formats, and displays the information appropriately. This program, however, needs to be run for upwards of 12+ hours - constantly handling a stream of incoming data. I am finding that as I let my app run for a while, the memory usage increases at a linear rate - not good for a 12 hour usage.

I have implemented a logger that writes the raw incoming binary data to a file - is there a way I can utilize this idea to clear my memory cache at regular intervals? I.e. how can I, every so often, write to the log file in such a way that the data doesn't need to be stored in memory?

Also - are there other aspects of a Windows Form Application that would contribute to this? E.g. I print the formatted strings to a textbox, which ends up displaying the entire string. Because this is running for so long, it easily displays hundreds of thousands of lines of text. Should I be writing this to a file and clearing the text? Or something else?

+2  A: 

Obviously, if the string grows over time, your app's memory usage will also grow over time. Also, WinForms textboxes can have trouble dealing with very large strings. How large does the string get?

Unless you really want to display the entire string onscreen, you should definitely clear it periodically (depending on your users' expectations); this will save memory and probably improve performance.

SLaks
Thanks much!The actual String object is maximally 17 characters long. Each time the string is appended to the display, nulled (string = "";), set to a new value and appended. Then rinse and repeat. So the actual string isn't getting too big, but the display within the textbox is because the string is constantly being appended to the current display. Does this change anything?
Slim
No. How often is the string updated? (How long does the textbox get?)
SLaks
The device is a 9600 baud rate constantly sending information. I use the _dataReceived() thread to handle incoming data - I'm not sure the speed details of what this works out to be, let's just say it's a lot and fast. Within a few minutes I have ~25K lines (each 17 chars long) being displayed.I did a quick test where every 50 iterations (of appending the 17char string to the display), I clear the display. This has shown TREMENDOUS improvements. It has currently been running for 17 minutes and has barely increased mem usage according to taskmgr. I think problem is solved.. thanks a ton.
Slim
Then you should accept this answer.
SLaks
done and done. sorry, adjusting to the way things work around here
Slim
No problem. Thanks.
SLaks
A: 

I have several serial applications which run either as an application or as a windows service. These are required to be up 24/7-365. The best mechanism I have found to avoid this same problem is two-fold.

1) Write the information out to a log file. For a service, this is the only way of getting the info out. The log file does not increase your memory usage.

2) For the application, write the information out to a log file as well as put it into a listbox. I generally limit the listbox to the last 500 or 1000 entries. With the newer .net controls, the listboxes are virtualized which helps but you also don't run into other memory issues such as the textbox concatenation.

You can take a system down with a textbox by constantly appending the string over a number of hours as it is not intended for that kind of abuse out of the box.

Mark