views:

436

answers:

3

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?

+3  A: 

Primitive data types (except string) are value types and are created on the stack and not the heap. They are popped off the stack when they go out of scope; they are not garbage collected.

Strings are reference types, are allocated on the heap, and are garbage collected. .NET performs some optimizations around memory management of strings using String Interning. (i.e. you will probably only have one instance of the string "" in memory. .NET can do this because strings are immutable)

foson
+1  A: 

With the two answers already given there isn't much I can add besides this article which gives a good description on how garbage collection works in .Net.

Gerrie Schenck
+5  A: 

Okay, with only two answers there's already misinformation...

  • String isn't a primitive type
  • String isn't a value type
  • Value type values aren't always created on the stack - it depends on where the variable is. If it's part of a class, it's stored on the heap with the rest of the data for that object.
  • Even local variables can end up on the heap, if they're captured (in anonymous functions and iterator blocks for example)
  • String literals such as "" are interned - they always resolve to the same string. That loop doesn't actually create any strings.

For more info, see my article on what goes where in .NET memory. You might also want to consider whether it's important or not.

Jon Skeet
String is a primitive type http://msdn.microsoft.com/en-us/library/aa711900.aspx
foson
No it's not: http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx "The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single." I trust that over the VB spec :)
Jon Skeet
And typeof(string).IsPrimitive definitely returns false. The C# spec only uses the word "primitive" twice, and in neither case does it define the term. ECMA-335 doesn't define it, but section 1.8.1.1 of partition III refers to boxing a primitive type value, which wouldn't apply to strings.
Jon Skeet
Again, you're correct, I spoke two soon, and made too sweeping a statement. I knew what I meant, but didn't communicate it well. Good answer (again) :)
Binary Worrier
So then for vb.net windows applications, since all code is on an object it's all likely to be on the heap, and the efficiency of creating an object that holds data vs a structure that holds the same is unlikely to matter at all? Besides the chances of passing a structure byVal and not getting data?
Maslow
No. Not everything is in an object (by the C# definition of "object" anyway - instance of a class, including the type used for boxing a value type). Not everything is on the heap - read the link. A local variable of a value type will *usually* store its data on the stack. (Continued)
Jon Skeet
There's certainly an efficiency point to be made - you wouldn't want for (int i=0; i < 1000000; i++) to pointlessly create a million objects to be garbage collected, for instance - but in my experience writing your own struct is rarely a good idea. See the class library design guidelines for more.
Jon Skeet
I'm very new to this forum, where do I find the class library design guidelines? I did a search on the forum and came up with many results, none of which look like an official FAQ or code repository for the site.
Maslow
Class library design guidelines: http://msdn.microsoft.com/en-us/library/ms229042.aspx Straight from MS, nothing to do with Stack Overflow :)
Jon Skeet