Results 1 to 14 of 14

Thread: [RESOLVED] [2005] .net Garbage collection and primitives

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Resolved [RESOLVED] [2005] .net Garbage collection and primitives

    Is the memory from primitive data types (int, char,etc) immediately released once they leave scope, or added to garbage collection for later release?

    consider:

    For x as integer=0 to 1000
    dim y as integer
    Next

    If this doesn't add 1000 integers to the garbage collector for clean up later,
    how does it treat string objects? would this create 1000 strings to clean up later?

    For x as integer=0 to 1000
    dim y as string=""
    Next


    How about structures that contain only int,string,etc... data types?

    Classes that contain only managed resources?
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] .net Garbage collection and primitives

    As far as I know, everything (all are objects) when out of scope or destroyed are added to garbage collection. How frequently the garbage collector runs to reclaim memory space depends on how much of free memory is left. The lesser the free memory left, the more frequent it will run to reclaim wasted space. It doesn't run, or runs at low priority until less than half the total memory available to it is left.

    I may be wrong though.
    Last edited by Pradeep1210; Jan 29th, 2009 at 04:07 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] .net Garbage collection and primitives

    It depends on if the variables are on the stack of the heap as to how they are effected by garbage collection and scope.

    The primitives you mention derive from ValueType, and when declared, they are pushed onto the stack, UNLESS they are delcared within the scope of a reference type (like a class) in which case they are pushed to the heap.

    So for example

    Code:
    Public Class SomeClass
        Dim MyInt as Integer 'WILL BE ON HEAP, IN SCOPE AS LONG AS SomeClass IS
    End Class
    
    Public Class SomeClass
    
    Public Sub DoSomething
        Dim MyInt as Integer 'WILL BE ON STACK, GOES OUT OF SCOPE WHEN DoSomething routine finishes
    End Sub
    
    End Class
    Objects on the heap are what the GC goes after.

    That is sort of a very simple explanation, but if you go look up memory management and the stack and heap, you should find some good resources that go into great detail on this.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] .net Garbage collection and primitives

    Thanks Kleinma,
    so then
    public structure Test
    public x as integer
    public y as integer
    end structure

    would be cleaned up and released as soon as it goes out of scope better than

    public class Test
    public x as integer
    public y as integer
    end class

    ?
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] .net Garbage collection and primitives

    In theory I would say yes, but again, I believe it matters WHERE the structure is declared and used as to whether or not it gets put on the heap and is subject to GC cleanup or not. I am really not an expert on it, but thats my understanding anyway.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] .net Garbage collection and primitives

    I'm in the habit of declaring anything as a class, making a sub new that stores all the data, and exposing the data as read-only properties.

    This helps enforce that the values are all set in a single line and a single location.

    My co-worker being schooled on assembly for hardware engineering thinks that taking each datatable row of data sticking the data into a class and reading it back after passing it to other methods is too inefficient for a desktop application. So he prefers structures for housing data in memory. My position is that is probably at most an extra 2 megs of memory for the applications we develop, I prefer the organization and structure imposed by readonly property based classes.

    Thoughts?
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] .net Garbage collection and primitives

    Quote Originally Posted by MaslowB
    I'm in the habit of declaring anything as a class, making a sub new that stores all the data, and exposing the data as read-only properties.

    This helps enforce that the values are all set in a single line and a single location.

    My co-worker being schooled on assembly for hardware engineering thinks that taking each datatable row of data sticking the data into a class and reading it back after passing it to other methods is too inefficient for a desktop application. So he prefers structures for housing data in memory. My position is that is probably at most an extra 2 megs of memory for the applications we develop, I prefer the organization and structure imposed by readonly property based classes.

    Thoughts?
    What happens if your structures are big?
    Structures are allocated on stack (unless within a class as described by Kleinma). Stack has a limited space and the primary purpose is to store small variables. Large objects should not be stored on stack, otherwise you may encounter StackOverflow exceptions in your application.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] .net Garbage collection and primitives

    Quote Originally Posted by MaslowB
    I'm in the habit of declaring anything as a class, making a sub new that stores all the data, and exposing the data as read-only properties.

    This helps enforce that the values are all set in a single line and a single location.

    My co-worker being schooled on assembly for hardware engineering thinks that taking each datatable row of data sticking the data into a class and reading it back after passing it to other methods is too inefficient for a desktop application. So he prefers structures for housing data in memory. My position is that is probably at most an extra 2 megs of memory for the applications we develop, I prefer the organization and structure imposed by readonly property based classes.

    Thoughts?
    Well you can make the same structure that you can with the class for those cases if thats what you want to do:

    Code:
    Public Structure ReadOnlyPoint
    
        Private _X As Integer
        Private _Y As Integer
        Public ReadOnly Property X() As Integer
            Get
                Return X
            End Get
        End Property
        Public ReadOnly Property Y() As Integer
            Get
                Return Y
            End Get
        End Property
    
        Public Sub New(ByVal X As Integer, ByVal Y As Integer)
            _X = X
            _Y = Y
        End Sub
    
    End Structure
    
    Public Class ReadOnlyPoint2
    
        Private _X As Integer
        Private _Y As Integer
        Public ReadOnly Property X() As Integer
            Get
                Return X
            End Get
        End Property
        Public ReadOnly Property Y() As Integer
            Get
                Return Y
            End Get
        End Property
    
        Public Sub New(ByVal X As Integer, ByVal Y As Integer)
            _X = X
            _Y = Y
        End Sub
    
    End Class

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] .net Garbage collection and primitives

    do you believe this saves memory? by declaring a structure instead of class for this scenario? or can you think of a way to test it?

    Is there a way to determine how much memory a particular class is using currently? If so I could write a test case easily.
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] .net Garbage collection and primitives

    I see little to no difference between using the class or the structure

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] .net Garbage collection and primitives

    The only place I would expect a significant performance difference between a class and a structure is if you pass them as an argument to a method. The class, being a reference type, will be nothing but the address of the argument. A structure, being a value type, will be pushed onto the stack during the call. If the structure contains members that are reference types, then you will only get the address of those members when the stack gets copied, but all value types will be copied directly. This means that the class will push a four byte address, while the structure will push all of its members or addresses of same. I have never seen it make any difference, though.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] .net Garbage collection and primitives

    Quote Originally Posted by Shaggy Hiker
    The only place I would expect a significant performance difference between a class and a structure is if you pass them as an argument to a method. The class, being a reference type, will be nothing but the address of the argument. A structure, being a value type, will be pushed onto the stack during the call. If the structure contains members that are reference types, then you will only get the address of those members when the stack gets copied, but all value types will be copied directly. This means that the class will push a four byte address, while the structure will push all of its members or addresses of same. I have never seen it make any difference, though.
    Doesn't this only apply if the two are passed byVal? if ByRef then there's no difference at all, correct?
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] .net Garbage collection and primitives

    There is a difference between the two when passed by ref, but in this context, a 32 bit address will be passed in both cases, so it is functionally the same. In the case of the structure, that address will be the address of the structure object, while in the case of the class, the address will be the address of the address of the class object. In both cases it will just be 32 bits, though (on a 32 bit system).
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] .net Garbage collection and primitives

    ok I think I've gotten my answer to this question, thanks =)
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

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