VB.Net and erractic memory handling??

  • Thread starter Thread starter Imogen
  • Start date Start date
Status
Not open for further replies.
I

Imogen

Guest
I have found that VB.net 's memory handling is very erratic at best.

Problem:
I built a small app in VB6 that pings a server periodically for information. The app uses approx 4500kb in memory. I built a similar app in .Net and it uses 14000kb! and as the program is working it gets larger...and larger.

Strange I thought, I must be allowing objects to live without disposing of the properly, I then went through all my code killing everything that wasnt nailed down. Still the same.

After a couple of days trying to get the program to conform, I noticed that if I minimised the main window to the task bar then set its state to normal again the memory usage would promply go down to 3000kb. As the program was working the memory usage would rise again, so I would perform the minimize...maximize (normal) trick again and yes...it went back to 3000k again.

At this point I tried playing around with the Garbage collector, fearing that it wasnt working properly, but that had no effect.

So in a nutshell I have a program that starts off using 14000kb (9500kb more than its VB6 equivilent) of memory until I minimize then maximize (normal) its main form, then the memory usage goes down by 11000kb!

The program runs for long periods (weeks/months) and the only way to stop it using vast amounts of memory is to periodically make the app automatically minimize then maximize!!

Now to me this seems rather odd! has anyone else noticed this?

Is this a bug?

Does anyone know a fix??

Thanks
Imogen

PS I have built the final exe's in release mode etc....
 
Have you established that your program actually *does* go on taking up memory forever if you just leave it?

This sounds very odd to me. I have deployed a large number of .NET applications, professionally and not. None of them have exhibited this behavior. Due to the nature of the .NET framework programs written it do tend to take up more memory, but the thousands of lines of code that went in to the Garbage Collector were not in vain. If it thinks that the memory your program is taking up is depriving resources from another process, it will collect some garbage.
 
i had this same problem with a count down timer i made when i first got .net. i could never figure out what the cause was either. it would eventually use so much memory on my friend's computer that it would slow his computer down. it's an interesting problem... sorry i can't help other than to give you some company. :)
 
When you say you are pinging another computer, are you using any non-managed code? Meaning, are you calling into a DLL or COM component? Maybe something in .NET is not allowing the object to release its memory.

I can't imagine why minimizing a form would release memory. The garbage collector should do its job either when things go idle or when too much memory has been used. If you'd like, you can always call the garbage collector yourself on a regular basic (once an hour or once a day may work for you). I believe it's just:
Code:
GC.Collect();

-nerseus
 
Just remember, the garbage collector doesn't always free 100% of
the memory. The GC only collects memory that isn't being used by
any objects.
 
The source code for the Garbage Collector is available. You don't have to take their word for it.
 
Well, this behavior also happens to the apps I've been developing, but the behavior when you minimize an app also ocurrs with IE, try it, load some pages, then minimize the browser, memory is released... the same thing that happens with .NET.
Now, I don't fully understand the GC, but I can't make the memory to get freed when the object gets disposed... I've tried GC.Collect(), XX = Nothing, etc etc etc and the memory keeps going up and up, I've used a memory profiler and see that sometimes, even if I dispose an object it never gets destroyed so the object keeps using memory, this is not correct I think, so ig anyone has a good code to destroy the objects (but really destroy them) I'll be glad cause the computers here don't have too much RAM to spare and I need to make the app use only the required memory
 
This question is along the same line.

I just finished coding a simple service in VB.NET. All the service does is receive UDP packets and write them out to a file. This service is used to collect stats on computer usage for reporting purposes. There is a client service that sends the UDP stat packets back to the master service.

Anyways, when started this service takes up 8.3Mb of memory with an additional 4.5Mb of Virtual Memory. I do not know if it "grows" over time, but I seriously doubt it.

btw, the service is coded in VB.NET and is around 30 lines of code with the following imports:

Imports System.ServiceProcess
Imports System.IO
Imports System.Net.Sockets
Imports System.Text.Encoding
Imports System.Threading

Is this the smallest memory footprint you can get with .NET (or specific to VB.NET) ??
 
Just a note... if you are using any classes in the drawing/drawing2d namespace and w98, please note:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnguinet/html/drguinet3_update.asp
[snippet]
Graphics, Pen, and Brush objects are all associated with Windows objects of a similar type. These Windows objects are allocated in the operating system's memory—memory that's not managed by the .NET runtime. Leave these objects allocated for long periods of time can cause performance problems, and can even cause painting problems under Microsoft Windows 98 as the graphics heap fills up. As a result, you should release the Windows objects as quickly as possible.

The .NET runtime will indeed free the Windows objects automatically when the corresponding .NET Framework object is finalized and garbage collected. However, it might take a long time for the garbage collection to happen—and all sorts of bad things, including filling up Windows heaps, can occur if we don't free these objects promptly. This would be especially bad in the ASP.NET version of this application since many users could be accessing the application on the same server box.
[snippet]
By the description of your problem, it doesn't look like you are using graphical stuff, but I've posted this just for info.
 
Well, I've been having the exact same problem (memory increasing more and more), with several .NET applications (all of them being "pure" .NET apps, no unmanaged code or dll)

don't know what to, except that minimize/maximize trick...
 
Does the application ever run out of memory because it is a well known fact that the garbage collector is inheritantly lazy and it wont clean memory unless required by another application.

So most the time you don't have to worry about it because if the system needs the memory back then the Garbage Collector will release the memory again.
 
Status
Not open for further replies.
Back
Top