Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Lenny on April 20, 2004, 08:23 PM

Title: Memory Leaks
Post by: Lenny on April 20, 2004, 08:23 PM
Is there anything in Visual Basic that can be done to solve this persistent problem in so many vb programs?

Perhaps some function that serves the same purpose as garbage collect in Java?  Something that could clear values in memory that no longer have a pointer?
Title: Re:Memory Leaks
Post by: MyndFyre on April 20, 2004, 08:54 PM
A Garbage Collector isn't a language feature, it's a runtime feature.  People need to remember that Java isn't only a language, but a platform.  It might be dependent on other platforms, but it is still one in its own right.  People call Novell a Network Operating System (NOS), but it doesn't run on its own.

However, I recommend you upgrade to VB .NET.  You do get implicit garbage collection -- no extra work required.  This is based on the .NET Platform though -- it is not a part of VB 6 or older.
Title: Re:Memory Leaks
Post by: Grok on April 20, 2004, 09:05 PM
Quote from: Lenny on April 20, 2004, 08:23 PM
Is there anything in Visual Basic that can be done to solve this persistent problem in so many vb programs?

Perhaps some function that serves the same purpose as garbage collect in Java?  Something that could clear values in memory that no longer have a pointer?

VB6 Memory Leaks?  Do tell!  Show me how you are creating these.
Title: Re:Memory Leaks
Post by: Lenny on April 20, 2004, 09:36 PM
After a long period of running my VB programs, they seem to build up more and more memory usage...Much faster than any other non vb programs....

I'm not storing any variables that consistently get larger and larger as time goes by, I assumed the large usage was due to storing values that are no longer used.....
Title: Re:Memory Leaks
Post by: Tuberload on April 20, 2004, 10:01 PM
Quote from: Lenny on April 20, 2004, 09:36 PM
After a long period of running my VB programs, they seem to build up more and more memory usage...Much faster than any other non vb programs....

I'm not storing any variables that consistently get larger and larger as time goes by, I assumed the large usage was due to storing values that are no longer used.....

Do you ever clear the rtb that stores the data recieved from battle.net? This was the main reason my bot took up so much memory. (When I programmed in VB that is)
Title: Re:Memory Leaks
Post by: Lenny on April 20, 2004, 10:06 PM
Actually, yes, my bot automatically removes the oldest lines of text once it reaches a user defined limit...
Title: Re:Memory Leaks
Post by: Tuberload on April 20, 2004, 11:07 PM
Quote from: Lenny on April 20, 2004, 10:06 PM
Actually, yes, my bot automatically removes the oldest lines of text once it reaches a user defined limit...

Geez, didn't mean to suggest something you're already doing. Next time I won't offer any help.
Title: Re:Memory Leaks
Post by: Lenny on April 20, 2004, 11:16 PM
That reply didnt seem hostile at all...
I was just mentioning it shouldnt be one of the causes of the growing memory usage...
Title: Re:Memory Leaks
Post by: Grok on April 20, 2004, 11:19 PM
Quote from: Lenny on April 20, 2004, 11:16 PM
That reply didnt seem hostile at all...
I was just mentioning it shouldnt be one of the causes of the growing memory usage...

So run it in debug mode and break out after a few hours to see what is taking up so much memory?  Try your local variables window, for starters.  I think it shows globals too.
Title: Re:Memory Leaks
Post by: Tuberload on April 20, 2004, 11:24 PM
Quote from: Lenny on April 20, 2004, 11:16 PM
That reply didnt seem hostile at all...
I was just mentioning it shouldnt be one of the causes of the growing memory usage...

I didn't say it sounded hostile, you just sounded offended I would even bring that up.
Title: Re:Memory Leaks
Post by: Adron on April 21, 2004, 05:07 AM
Quote from: Grok on April 20, 2004, 09:05 PM
VB6 Memory Leaks?  Do tell!  Show me how you are creating these.

You can easily create memory leaks in VB. All you have to do is make some objects that reference each other and then drop your own references to them.
Title: Re:Memory Leaks
Post by: iago on April 21, 2004, 09:07 AM
hmm, incidentally, I'm going to move this to VB forum
Title: Re:Memory Leaks
Post by: Telos on April 21, 2004, 10:23 AM
Ive found that if youre optimizing your VB programs for memory usage encapsulating everything in classes is the best way to go but you incur a performance penalty for adding a layer of abstraction.  The other thing I do is to explicitly remove references to class objects when Im through this way I dont end up with looped objects as Adron described
Title: Re:Memory Leaks
Post by: Grok on April 21, 2004, 12:31 PM
Quote from: Adron on April 21, 2004, 05:07 AM
Quote from: Grok on April 20, 2004, 09:05 PM
VB6 Memory Leaks?  Do tell!  Show me how you are creating these.

You can easily create memory leaks in VB. All you have to do is make some objects that reference each other and then drop your own references to them.

What's so easy about it?
Dim objX As Object, objX1 As Object, objX2 As Object
Dim intMod2 As Integer
For lPos = 1 To 100
   Set objX = CreateObject("Project.Class")
   intMod2 = lPos Mod 2
   If intMod2 = 1 Then
       Set objX1 = objX
   Else
       Set objX2 = objX
       Set objX1.otherguy = objX2
       Set objX2.otherguy = objX1
   End If
Next lPos
Set objX1 = Nothing
Set objX2 = Nothing


You have to get pretty sloppy to do that?
Title: Re:Memory Leaks
Post by: Eric on April 21, 2004, 12:33 PM
Quote from: Lenny on April 20, 2004, 09:36 PM
After a long period of running my VB programs, they seem to build up more and more memory usage...Much faster than any other non vb programs....

I've ran my Visual Basic-made programs for weeks at a time without experiencing any problems.
Title: Re:Memory Leaks
Post by: Adron on April 21, 2004, 02:36 PM
Quote from: Grok on April 21, 2004, 12:31 PM
You have to get pretty sloppy to do that?

If you build yourself a huge tree of objects that reference each others in intricate ways, I can see a loop happening easily. Circular references happen naturally when you get lots of objects notifying each other about things.

Edit:

Other example: Create a binary tree where each node is an object and has pointers to its child nodes and parent node so you can walk the tree any way you like. Eibro has a C++ one of those as in his assignment for Skywing.

If you make such a structure in VB and don't walk through the tree removing the nodes when you want to get rid of the structure, you've made yourself a memory leak.