Solved

Garbage Collection In CollectionBase Classes

Posted on 2007-11-29
9
295 Views
Last Modified: 2013-11-26
I'm having a huge problem.  I have created a CollectionBase class called Photos.

Photos is a collection of Class Photo. and holds (go figure) a photo and some information about the photo (name, size and such)

The photos are stored as Blob data in a SQL database and Photos has a load method that loads the collection from the DB.

Ok... as a test i tried this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Photos As New colPhotos

        Dim icount As Integer
        For icount = 1 To 10
            Photos.Load()
            Me.Label1.Text = Photos.Count
            Photos.Clear
        Next icount
        Me.Label1.Text = "Done"
        Photos.Dispose()
    End Sub

Now when I run this I watch the windows task manager.  The first iteration uses up about 15mb of memory,  The problem is that each susequent iteration uses another 15mb of memory.

when the loop finishes i've got 150+ mb of memory used that will not be returned until i close the program. In this test case a single form.
Nothing i have tried will release the memory untill the program exits.... I even tried waiting an hour to see if GC would eventually release it.
Tried Early binding ..  LateBinding
I've even tried :
       System.GC.Collect()
        System.GC.WaitForPendingFinalizers()

Why is VB holding onto that memory. I'm not creating another instance of the object (at least I think I'm not)
Anyone know how to get VB to release the memory.
I have several of these classes in an app and I noticed over time the mem utilization going Up...Up..Up


0
Comment
Question by:mnichols1202
  • 5
  • 2
9 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
Are you using any classes that implements IDisposable, like Image? Then you have to make sure that you call the Dispose method when removing them. Otherwise they will be hard to garbage collect, and they may leak system resources like windows handles. If you don't call Dispose, the objects have to be finalised before they can be collected. The finalisation is executed in a background thread, so if you leave a lot of object undisposed, it will take quite a while before they are finalised and can be collected.

Other than that, it's normal for a .NET application to allocate memory and don't free it unless it's needed. If the system needs the memory back, the garbage collector will make a more thorough collection.

Forcing a garbage collection is not something that you normally would do. The garbage collector handles this best on it's own. Also, calling the Collect method doesn't guarantee that all collectable objects are collected. The garbage collector decides for itself what objects are efficent to collect at any given time.
0
 
LVL 8

Expert Comment

by:Autoeforms
Comment Utility
just curious have you minimized and restored your app.
the footprint is normally reduced doing this. if yours is not something is still holding on to the memory
good luck these problems are pup
greg
0
 

Author Comment

by:mnichols1202
Comment Utility
GreenGhost:
You Say: "Other than that, it's normal for a .NET application to allocate memory and don't free it unless it's needed. If the system needs the memory back, the garbage collector will make a more thorough collection."

The problem is that a .net app running on a windows machine is not running in a vacume.  I've seen a .net collection run.up 2GB of memory when the machine only has 512mb of real RAM. Obvously memory is "needed' by the system. by this .net application is hanging on to 1.9gb of what is supposed to be released memory.
After further investigation it seems that the problem is in the CollectionBase Clas itself not relaeasing memory when the .Clear method is call on the list.

What about a byte array. would this have the same collectin problems as say an image object?





0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
> the problem is in the CollectionBase Clas itself not relaeasing memory when the .Clear method is call on the list.

The Clear method is not supposed to release any memory, other than the objects that it is referencing (if it is a collection of reference types).

The Clear method sets all items to 0/null and sets Count to zero, but it doesn't change the Capacity of the collection. If you want the underlying array to be freed, you should set the Capacity to zero also.

> What about a byte array. would this have the same collectin problems as say an image object?

An array in itself is a managed object and doesn't need any disposing. (If it is an array of references to objects that are disposable, the objects themselves of course needs disposing.)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
Checking back to see if you had any progress?
0
 

Author Comment

by:mnichols1202
Comment Utility
I did - I found out that for some reason you must use the ERASE function to get a byte array to release it's memory for garbage collection.  Anyone feel like commenting as to why it works that way?
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
You don't need to use Erase, setting the reference to Nothing does the exact same thing.

From MSDN Library:

"The Erase statement is equivalent to assigning Nothing to each array variable."

I tried this, and the statement

Erase a

actually compiles into the exact same code as:

a = Nothing


An object is available for garbage collection when it can not be used any more. For member variables this is when the reference is set to Nothing (or when the parent object is available for garbage collection).
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
Comment Utility
TheLearnedOne, why do you think that the question should be deleted?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now