views:

115

answers:

6

Memory leak is, when there is unused memory in application and GC can collect it, normally it occurs if some where in application we keep unwanted strong reference of an object, and GC will able to find the path(Direct and indirect) so it can free this object, but this all are true about reference type that mean in Heap memory allocation.

But what about stack And as far i know GC will not responsible to clean up the stack it will automaticaly clean when function will return.

So my question is that Is there any chance that memory leak will occurs in stack also?, if yes then at what scenario and what are the best practices to avoid this kind of leak.

A: 

Variables that allocated in stack are destroyed after block ends. So it is not possible that memory leak can accrue. Bed practice is to create a variable that is used only in a given block outside of that block

ArsenMkrt
A: 

As you say the GC doesn't clean up the stack. However, the space used by the stack is reclaimed during unwind. So as the stack is popped the stack pointer is moved "back" and thus the space can be reused.

Running out of stack space will raise the StackOverflowException. It is usually due to a programming error with unlimited recursion.

Brian Rasmussen
A: 

Without knowing too much about the internals of .NET I would say that if you have a stack-leakage you will have a crashing program as the function/method return address is stored on the stack as well (most likely anyway).

If your stack gets misaligned somehow you would have far bigger problems than a memory leak.

Usually method parameters and return values are stored on the stack (if not optimized to be sent via cpu registers).

Subtwo
A: 

No, as long you are using safe code, memory leaks cannot occur for stack variables.

logicnp
+2  A: 

If you are writing recursive functions that keep stack-local references to large data that is not needed in sub-recursive calls, then this is a type of space leak, but it is very unusual to have this be a problem in practice.

More generally, if you have something like

Main() {
    var s = ReadInAGiganticString();   // say 10 Megs long
    Server(s.Substring(0,5));          // but I only care about first 5 chars
}
Server(s) {
    while(true) { ... }                // but 10M is on stack forever
}

then this is a kind of stack-space-leak, but again, it is unlikely in practice. The fix is easy:

Main() {
    var s = ReadInAGiganticString();
    var t = s.Substring(0,5);
    s = null;                         // the fix
    Server(t);
}
Server(s) {
    while(true) { ... }
}

In general, if you ever have a giant variable on the stack right before a call that will last 'a long time' and the variable is no longer used, you can null it out to ensure it can be GC'd before going into the 'long' call.

(It is possible that an optimizer may do this for you based on reachability analysis.)

Note that the above answer is for references to heap-allocated objects that are rooted on the stack. If, on the other hand, you have stack-allocated memory (e.g. a gigantic struct or stackalloc thingie), then the fix is not as easy, but this is even rarer in practice (who ever creates giant structs?).

Brian
Only the reference to the string is on the stack, the string itself resides on the heap.
Ritsaert Hornstra
+1  A: 

A memory leak can happen if you fail to free unmanaged resources correctly. So I am afraid that if you send a managed object to a method and this object wraps an unmanaged resource and it fails to free the unmanaged resource correctly there will be a memory leak. The stack will free the managed reference but the unmanaged resource will stay alive.

Ikaso