Results 1 to 11 of 11

Thread: garbage collection with .net

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved garbage collection with .net

    If I create an instance of a business class :
    Code:
     Dim lclsEmployee = New clsEmployee
    Do I have to worry about clearing it out when I am finished with it ?
    Last edited by venerable bede; Sep 22nd, 2004 at 09:40 AM.

    Parksie

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,633
    No, the garbage thing does that for you.

    However, if you are like me, and place code in the terminate event (VB6) of the class then this will annoy you.

    The event in VB.NET is the Dispose event.
    To run code when the class terminates you must override this dispose event

    Not too happy with it myself yet.

    Woka

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I've never used VB 6.o so you've lost me Woka.

    Wuff

    Parksie

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,633
    U've never used VB6?
    But you post in the VB 6 section...in fact, what the hell are you doing on the site you deranged Georgie

    In VB6:
    clsEmployee
    VB Code:
    1. Private Sub Class_Initialize()
    2.    MsgBox "Woof"
    3. End Sub
    4.  
    5. Private Sub Class_Terminate()
    6.    MsgBox "Badgers!"
    7. End Sub
    Then in a form:
    VB Code:
    1. Dim objSomeone As clsEmployee
    2.    Set objSomeone = New clsEmployee 'This cause the initialise event to fire, and a woof msgbox.
    3.    
    4.    Set objSomeone = Nothing 'This causes the terminate event to fire, and a badger msgbox.
    Does that make sense?

    Anyways, .NET doesn't work like this.
    You have new instead of Initialise.
    VB Code:
    1. Public Sub New()
    2.    'code here when object is created
    3. End Sub
    This has more potential as you can do:
    VB Code:
    1. Public Class clsEmployee
    2.    Public Sub New(ByVal EmployeeID As Long)
    3.       'code here to laod Employee from ID
    4.    End Sub
    5. End Class
    Then in a form you can do:
    VB Code:
    1. Dim woof As New clsEmployee(54)
    2. Dim woof As clsEmployee = New clsEmployee(67)
    both lines do essentially the same thing.

    Woof

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I already know that.

    Woof.

    I never post to the VB 6.0 forum. You must have me on you're mind a lot you perv badger.

    Parksie

  6. #6
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Although there is a garbage collector it should not be regarded as not trying to clean up memory. In fact the way the garbage collector works is much like java's garabage collector.

    You should use try catch finally blocks and close your db connection as well as release resources in the finally block

    try

    catch

    finally
    myConn.close()
    myConn = Nothing

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by jhermiz
    Although there is a garbage collector it should not be regarded as not trying to clean up memory. In fact the way the garbage collector works is much like java's garabage collector.

    You should use try catch finally blocks and close your db connection as well as release resources in the finally block

    try

    catch

    finally
    myConn.close()
    myConn = Nothing
    You only have to do that with resource intensive objects such as the connection object, image object, etc...

    All others just let them go out of scope, much easier and it really has no impact on performance.

  8. #8
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Don't let any of those crazy c++ programers hear you say that, Hellswraith.
    Magiaus

    If I helped give me some points.

  9. #9
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Magiaus
    Don't let any of those crazy c++ programers hear you say that, Hellswraith.
    Only reason they would have a problem with that is if they don't understand the garbage collection process.

    Once a object is allocated in memory, that space isn't freed up unless the garbage collector frees it up. It doesn't matter if you set the reference to null before you leave the method or if you let it drop out of scope, both produce the same action as far as the garbage collector is concerned.

    Database connections though are different. They hold references to unmanaged resources. This means you need to call the .Close() methods or .Dispose() methods of those types of objects so the object itself can release the resource it is using immediately instead of waiting around for the garbage collector to come along and free it up by destroying the connection object which is no longer referenced by your app.

  10. #10
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Very true. I'll go ahead and mention, just for the heck of it, that if you do doubt this you can call System.GC.Collect(), but it isn't recommened because it requires the extra proccess time.

    The only reason I said anything is because I was reminded of a thread with CorneedBee talking about how arrays are so ineficient if you ReDim. You may remember it you explained about creating a ReDim function the allowed you to preserve an existing array.

    I always trust your advice my friend.
    Magiaus

    If I helped give me some points.

  11. #11
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Magiaus
    I always trust your advice my friend.
    That is scary, since even I don't trust my own advice...lol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width