[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?
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.
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.
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
?
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.
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?
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.
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
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.
Re: [2005] .net Garbage collection and primitives
I see little to no difference between using the class or the structure
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.
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?
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).
Re: [2005] .net Garbage collection and primitives
ok I think I've gotten my answer to this question, thanks =)