Hi,

I am new in List<string> ,Memory leak,.net,

I can i manage memory for List<string>

e.g.

List<string> list=new List<string>();

list.Add("a");
list.Add("b");
list.Add("c");
list.Add("d");
list.Add("e");

list.Clear();

in above example ,will List.Clear() release memory for list or items.

Thanks and Regards
Shabab Begym

Recommended Answers

All 7 Replies

.NET does not manage memory like other traditional languages. The .NET CLR has a garbage collector that runs periodically to destroy objects and free memory once all references to the object have gone out of scope or are otherwise list.

You should familiarize yourself with Garbage Collection:

You can manually call the garbage collector to force collection but this is against .NET best practices. The memory from your code will be released by the CLR in the next GC cycle.

Also please use code tags when posting code on daniweb:

commented: Well said :) +6
commented: Happy 2010 To U! +6

Memory leaks occurs when memory is allocated in a program and it is never returned to the OS, even that a program does not use the memory any longer.

What scott said "Because of the garbage collection, it is not possible to have memory leak in managed code." A memory leaks occurs in a .net application if unmanaged code as part of that application.

Thanks for reply

That's right which you all have said

but first, i would like to know exact participation of "List<string>()" object in memory leak if i will not Clear() it ,will it generate a memory leak?

Second, will object of any class ,which contains object of a String() or object of List<string>() , generate memory leak?

for example

class Class1    
 {
       public List<string> list = new List<string>();
       private string str1="";
 }


    public partial class Form1 : Form
    {
        private Class1 obj=new Class1();;
        public Form1()
        {
            InitializeComponent();
            CreateList()
        }
        private void CreateList()
        {
            obj.list.Add("a");
            obj.list.Add("b");
        }
    }

If it will generate memory leak then how can i solve it.

As sknake and adatapost already pointed out, don't worry about memory leaks if you write managed code. What you wrote was managed code. So concentrate on programming, not on memory leaks.
Forget about dispose, destructors etc. The Clear method just removes all items from your List.

But i tested my application on ANTS Memory Profiler 5.1 which is showing me some memory leaks related with List<string>

Its not leaking memory. List<T> and string are managed data types. Also in your code you are calling obj.list.Add("a"); which is not defined in the code you posted so it looks like that variable is declared application-wide? Remember that as long as you hold a reference to a variable then it will not be garbage collected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.