I am not able to understand few things on the Garbage collection.
Firstly, how is data allocated space ? i.e. on stack or heap( As per my knowledge, all static or globa
Firstly, how is data allocated space ? i.e. on stack or heap( As per my knowledge, all static or global variables are assigned space on stack and local variables are assigned space on heap).
No, stack variables are method calls and local variables. A stack frame is created when the method is called and popped off when it returns.
Memory in Java and C# is allocated on the heap by calling "new".
Second, GC runs on data on stacks or heaps ? i.e a GC algorithm like Mark/Sweep would refer to data on stack as root set right? And then map all the reachable variables on heap by checking which variables on heap refer to the root set.
GC is used on the heap.
Mark and sweep would not be considered a cutting edge GC algorithm. Both Java and .NET GC use generational models now.
What if a program does not have a global variable? How does the algorithm work then?
What does "global variable" mean in languages like Java and C# where everything belongs to a class?
The root of the object graph is arbitrary. I'll admit that I don't know how it's chosen.