Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Memory Management

Update: I probably confused memory usage issues with the UI sharing same thread as the processing (as pointed out by MusiGenesis below). However regarding the Memory usage. I am still not able to find VB.net specific syntax, although people have pointed out some great .Net and C# information below (and if I were more versed in those technologies, one could adapt to make work with VB.net).

I am creating a VB.Net application.

  • The application basically Parses Data Files located on the client machine into DataSet/DataTables.
  • Then using DataView, it breaks down the DataTables into manageble chunks, writes to XML and sends the XML data to a webservice.

The general concepts are working fine, however I am having issues where the Mem Usage on Task Manager keeps growing as the program is used to load more and more files.

On Startup, before doing anything, the VB application has 27,000 K. Once the file is parsed and even after I dispose of the File handle as well as the the data increases a lot. I strip out everything in the code and it still seems that memory in Mem Usage remains captured. There is no rhyme or reason as to why the Mem Usage is growing (i.e. sometimes it can grow by 20 mb when reading a 7mb file, however other times it does not increase at all, when reading a 3mb file). Sometimes, it appers to release some memory when the parsing is complete and other times it just holds.

I have looked at .Net Memory Profiler and have not really been able to make heads or tails from that.
I have read a lot on internet regarding Memory Management on .Net in General about Dispose and "Nothing" and DataSets, etc, however have not really found anything regarding VB.Net specifically.

My General Question is: Are there any good tutorials/books/blogs/etc that show a more in depth tutorial on managing memory in a VB.Net application (i.e. how/when to dispose/close, etc), or does anyone have some specific tips from there experience.

like image 981
Brian Avatar asked Dec 29 '22 15:12

Brian


1 Answers

First, you need to realize that Task Manager is showing you the amount of memory the operating system has allocated to your application. This is not necessarily the amount of memory actually being used. When a .NET application first starts, the operating system allocates memory for it, just as it does for any process. The .NET runtime then further divides that memory and manages how it is used. The runtime can be thought of as "greedy" in that once allocated memory by the operating system it won't give it back unless specifically asked to by the operating system. The result is that the memory usage in Task Manager is not accurate.

To get a true picture of your memory usage, you need to use Performance Monitor and add the appropriate counters.

As far as IDisposable and the dispose pattern, you probably won't find much that talks about this in language specific terms since it is something provided by the .NET Framework itself and is language agnostic. The pattern is the same no matter what language you use, only the syntax is different.

There are several references available that will give you information on how memory management works. I have two blog posts, one which talks about Using Garbage Collection in .NET and one which lists the various resources I used to create two presentations on memory management in .NET.

The best "rule of thumb" is that if a class implements IDisposable, it does so for a reason and you should ensure that you are calling Dispose() when you are done using the instance. This is most easily accomplished with the using statement.

like image 193
Scott Dorman Avatar answered Jan 04 '23 22:01

Scott Dorman