1
struct a
{
   public string str;
}

a bb = new a();

class a
{
   public string str;
}

a bb = new a();

Is it correct to say that classes are always garbage collected? Are structs kept in memory (forever)?

ouflak
  • 2,458
  • 10
  • 44
  • 49
testing
  • 79
  • 1
  • 7
  • 4
    I really don't understand what are you asking. What does it mean “supports garbage collection”? Is there some specific reason why are you asking this? – svick Jan 08 '13 at 17:37
  • Your question doesn't make sense as-is. If you could give us an example, and what you expect to see, maybe we could help you better. – Kendall Frey Jan 08 '13 at 17:39
  • 1
    Is your concern that the struct will forever consume memory? That is certainly not the case. – Jon B Jan 08 '13 at 17:40
  • 1
    Same as [Destroying a struct object in C#?](http://stackoverflow.com/questions/2146434/destroying-a-struct-object-in-c) – mrtgold Jan 08 '13 at 17:40
  • In case of class it get memory at runtime. right? My question is - using struct also needs memory in RAM for execution. correct? – testing Jan 08 '13 at 17:43
  • @testing Of course. Every C# data structure is stored in RAM. – Kendall Frey Jan 08 '13 at 17:45
  • @KendallFrey - as class gets memory , objects of structs also gets memory. means on heap correct ? – Pankaj Jan 08 '13 at 17:46
  • @abcdefghi Not all data is stored on the heap. Local variables and arguments are stored on the stack. – Kendall Frey Jan 08 '13 at 17:48
  • @KendallFrey [All data is stored in virtual memory, which may or may not map to RAM](http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx). – Servy Jan 08 '13 at 17:49
  • The basic meaning of heap is that it contains some memory address and whereas the stack contains direct value. that's it? – testing Jan 08 '13 at 17:52
  • @testing Not exactly. Perhaps you should do some reading on the stack and the heap. The heap holds objects that are referenced by the code. The stack holds local variables, which either reference an object on the heap, or store a value (such as int) directly. – Kendall Frey Jan 08 '13 at 17:54
  • can you tell a video link/text that explain in details about these terminology ? – testing Jan 08 '13 at 17:55
  • @testing Perhaps you should go to Google or Wikipedia. – Kendall Frey Jan 08 '13 at 17:57

4 Answers4

5

EDIT -> Answer updated following discussion on comments, and link shared by Rob

struct are value types. Usually No separate memory is allocated for them on heap. Usually No need of Garbage Collection.

There are however exceptions. Memory allocation is not guranteed to be stack allocated for value types or heap allocated for reference type. Read this answer to other SO question and The Stack in An Implementation Detail for detail information.

If struct has some reference type as member variable, then the reference type will be garbage collected (in next garbage collection trigger) once struct goes out of scope and the reference type has no more accessible roots to it.

If your example, you have used string as reference type. String are handled differently using intern pool.

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
  • What about if struct is a object field? – Ilya Ivanov Jan 08 '13 at 17:38
  • 2
    So, if I have an array of `struct`s, where is that allocated? – svick Jan 08 '13 at 17:39
  • Do you mean they will not be allocated on heap? – Ilya Ivanov Jan 08 '13 at 17:40
  • @svick, Array is a class and is reference type. – Tilak Jan 08 '13 at 17:40
  • As mentioned, if struct has object field, that object will go to heap, struct will remain on stack. – Tilak Jan 08 '13 at 17:42
  • In case of class it get memory at runtime. right? My question is - using struct also needs memory in RAM for execution. correct? – testing Jan 08 '13 at 17:43
  • just in case - not all structs are stored in stack. You can simply cast literal `5` to `object` (`box` it) and then it will be stored on heap and will be subject to `GC` – Ilya Ivanov Jan 08 '13 at 17:46
  • @LLya But then there are 2 items, literal 5, and boxed object. literal 5 is on stack, boxed object (separate entity) is on heap. Let me know if i am not clear. Not all structs are stored in stack. This is true for exceptional cases (but not your just in case example)Example, allocating memory in unsafe blocks. – Tilak Jan 08 '13 at 17:48
  • @testing, I think you mean process virtual memory by RAM. Read [this](http://www.codeproject.com/Articles/76153/Six-important-NET-concepts-Stack-heap-value-types) – Tilak Jan 08 '13 at 17:51
  • The basic meaning of heap is that it contains some memory address and whereas the stack contains direct value. that's it? – testing Jan 08 '13 at 17:53
  • This answer is wrong. Structs are not always allocated on the stack. See e.g. http://stackoverflow.com/questions/4853213/c-sharp-struct-stack-allocated-or-sometimes-heap-allocated – Rob Jan 08 '13 at 17:53
  • @Rob thanks, I hope this link closes our discussion here – Ilya Ivanov Jan 08 '13 at 17:54
  • Answer updated accordingly. – Tilak Jan 08 '13 at 18:16
4

Yes. If struct is a field of a object of reference type, then it is stored in the heap, then it is subject to Garbage Collection

Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • you mean like this ? `struct a { public object str; }` – testing Jan 08 '13 at 17:38
  • @testing no, `class Foo { private struct Bar; }` – Ilya Ivanov Jan 08 '13 at 17:39
  • In case of class it get memory at runtime. right? My question is - using struct also needs memory in RAM for execution. correct? – testing Jan 08 '13 at 17:44
  • @testing [Not quite. Creating a struct will consume virtual memory, but that may or may not map to RAM](http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx). – Servy Jan 08 '13 at 17:52
  • The basic meaning of heap is that it contains some memory address and whereas the stack contains direct value. that's it? – testing Jan 08 '13 at 17:54
  • 1
    @testing Stop asking that question. You have gotten multiple responses in several comments. – Kendall Frey Jan 08 '13 at 17:56
2

If your question is whether the string will remain in memory in the struct example, the answer is no. Members of structs are subject to garbage collection when they leave scope, just like any other objects.

The .NET GC uses a mark-and-sweep approach, where it examines objects that are pointed to by static fields, existing objects, and local variables on the stack, among others.

Since structs in local variables are located on the stack, they are swept as normal. Structs in object members on the heap are also swept as the GC goes through the object tree. Same goes for static members.

In short, structs are swept in the same manner as classes. The only difference between them is the way they are stored. Structs are stored per-variable, while classes are stored as a reference. Both ways are subject to the GC.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • The basic meaning of heap is that it contains some memory address and whereas the stack contains direct value. that's it? – testing Jan 08 '13 at 17:53
0

If I know correctly, yes in C# they both are Garbage Collected.

Whether stored in heap or stack depends on lifetime of the variable/instance not the type.

One thing to consider is when more complicated situations such as subscribed events handler resides in the instance of class, in that case Garbage Collector can skip collecting and the instance can resides in memory. But above examples will probably be collected since only one string type declared.

swcraft
  • 2,014
  • 3
  • 22
  • 43
  • 2
    The purpose of the garbage collector is to manage the memory allocated on the heap; it allocates new memory when requested for new allocation and is responsible for keeping track of what memory is currently "used" so as to not provide that memory to anyone else while they're still needed elsewhere. Information stored on the stack is not within the "scope" of the GC; the GC is not used for either allocation or deallocation. Both objects and structs (in different situations) involve allocation of stack and heap memory, in different ways. – Servy Jan 08 '13 at 18:06