Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Garbage Collection ever affect stack?

Tags:

java

c#

.net

I have just started with .NET Framework with C# as my language. I somewhat understand the concept of GC in Java, and had a revisit to the same concept in .NET today.

In C#, the value types are put onto the stack(same as the case with Java,where local variables are put onto the stack). But in C#, even struct is included in value types. So, even structs are placed onto the stack. In a worst case scenario, where there are many method calls, and the stack is populated heavily with many methods, and each method has many local value types, and many structs that themselves have many local value types, will the Garbage Collector ever affect the stack? From what I researched(and partly what I was taught about), I understand that it won't do so. Primarily because manipulating stack content will involve a lot of overhead, and besides, GC only consults stack to lookup for references - and nothing more than that.

Just to add another question related on the same topic : Forcing a call to GC(like System.gc() in Java, not sure about the C# equivalent), doesn't ensure that the GC routine is called then and there. So where should I place such a call - where I expect that I need the GC to run, or any random place as there is no guarantee that my call would immediately trigger the GC? Or should I just leave the stuff to the Runtime Environment and not bother about it?

Note: I added the Java tag because I'm trying to link concepts from there. I understand that the internal functioning of GC in the two separate Runtime Environments will definitely be different, but I guess the underlying concept would be the same.

like image 805
Kazekage Gaara Avatar asked Jul 26 '12 14:07

Kazekage Gaara


3 Answers

No garbage collect does not affect objects on the java stack.

GC only affects objects in the jvm's heap. The java GC process is multi-tiered and can be very complex, and worth reading up on. Check out a site like: http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html to get a good grasp on how it operates.

As far as forcing the system's GC that is a bad idea. The jvm will have a better idea that you when a GC needs to run. If you are attempting to allocate a big object, the jvm will ensure the space is there for you without you needing to tell it to run the GC.

EDIT My bad, you are more concerned about C# than java. The same principals of memory management apply, stack is unaffected, don't explicitly run a GC, etc. C# is designed to operate in a similar manor to java. http://msdn.microsoft.com/en-us/library/ms973837.aspx

like image 113
Noah Avatar answered Oct 11 '22 16:10

Noah


Stacks don't need the assistance of a garbage collector; because, as you move out of stack frames (the scope of the current execution within the stack), the entire frame, including contents, is freed (and overwritten) as you create a new stack frame.

function foo(int a, int b) {
  int i;
  doStuff();
}

creates a stack frame (rough visualization)

---- Frame Start ----
(value for parameter a)
(value for parameter b)
(other items needed for tracking execution)
(extra stack frame space
  (value for stack allocated i)
)
---- End of Frame ----

When entering a function, stack allocated variables are allocated as the frame is allocated, when exiting the frame, the entire frame is discarded, deallocating the memory for frame allocated variables.

Keep in mind that Java typically allocates object references and stack local primitives on the stack, not whole objects. Only a few recent optimizations permit in-stack allocation of objects not reachable outside the frame; which has such conditions on it that it is not considered something you can count on.

That said, references in the stack frame typically point to the heap, which is garbage collected normally.

like image 44
Edwin Buck Avatar answered Oct 11 '22 16:10

Edwin Buck


If you read this for .NET, it only works on the managed heap:

http://msdn.microsoft.com/en-us/library/ee787088.aspx

MSDN seems to be a treasure trove of information, here is the parent topic on the GC in the CLR:

http://msdn.microsoft.com/en-us/library/0xy59wtx

like image 1
Daniel A. White Avatar answered Oct 11 '22 16:10

Daniel A. White