Click here to Skip to main content
15,917,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a global variable int [] t, and the following function, now calls the function multiple times, the length different every time,this action will cause a memory leak it?

C#
static int[] t; // global variable

void testIt(int length)
{
    int[] temp = new int[length];
    for(int i=0;i<length;i++)
    {
        temp[i]=i;
    }
    t = temp;
}
Posted
Comments
Sergey Alexandrovich Kryukov 3-Aug-13 22:12pm    
There is no such thing as "global variable".
—SA

First of all, please disregard Solution 1. Unfortunately, this member has no clue on this and some related topics.
And please see my comment to the question: there is no such thing as "global variable" in .NET (and this is so-o good!).

Don't worry, it cannot cause a memory leak in principle. Memory use in managed systems is controlled by the Garbage Collection. It comes into play when some object becomes unreachable. This is explained in more detail here:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

That said, it does not mean that memory leaks are impossible in managed systems. They are possible! However, "accidental" memory leak are mostly prevented. The memory leaks people really create are more of the wrong general design of the code. I explained it in my past answers:
Memory leak in WPF DataBinding[^],
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
deferring varirable inside the loop can cuase memory leak?[^],
Garbage collectotion takes care of all the memory management[^].

—SA
 
Share this answer
 
v2
Comments
Ron Beyer 3-Aug-13 22:27pm    
+5, most true memory leaks in .NET are created when calling unsafe code, other than that, the biggest real (easy) danger of memory "leaks" are with event handlers.
Sergey Alexandrovich Kryukov 3-Aug-13 22:33pm    
That's true.
Thank you, Ron.
—SA
Yes it cause memory leak.

when first time function is called than memory allocated at heap and addressed returned to temp which is assigned to t,
now when second time this function is called again new memory is created at heap and address is returned by new which is different from previous one and assigned to t.

so first heap memory address which is save in static variable t is lost which is not deleted from heap. Hence memory leak concurred.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-13 22:11pm    
No, it cannot cause the leak in principle. And there is no such thing as "global" in .NET.
You should not give advice on the topics when you have no clue at all.
—SA
Ron Beyer 3-Aug-13 22:24pm    
+2 for Sergey's comment, purely by the way the garbage collector works, and how references work in .NET, this cannot leak. You do not have to explicitly delete from the heap in .NET, the GC reference counter takes care of it. Its actually very difficult to "leak" memory in .NET like you do in C/C++.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900