Solved

Garbage Collection In CollectionBase Classes

Posted on 2007-11-29
9
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
9 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20381251
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
ID: 20381285
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
ID: 20382138
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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20383638
> 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
 
LVL 29

Expert Comment

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

Author Comment

by:mnichols1202
ID: 20462112
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
ID: 20462806
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
ID: 20594267
TheLearnedOne, why do you think that the question should be deleted?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

630 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