System.Collections.Generic.List<>

Use this forum to discuss and/or suggest future improvements of .NET Memory Profiler and to report bugs.

Moderator: SciTech Software

System.Collections.Generic.List<>

Postby scot.prichard@med.ge.com » Mon Mar 13, 2006 9:15 pm

Why does the list appear as a leak between MemSnapShot 2/3 but the array between MemSnapShot 1/2 does not?
-----------
using System;
using System.Collections.Generic;
using System.Text;

namespace test0313
{
class Program
{
private class Leak
{
}

private static void TestArray()
{
Leak[] array = new Leak[10];
for (int i = 0; i < 10; i++)
array[i] = new Leak();

for (int i = 0; i < 10; i++)
array[i] = null;
array = null;
}

private static void TestList()
{
List<Leak> list = new List<Leak>();
for (int i = 0; i < 10; i++)
list.Add(new Leak());

for (int i = 0; i < 10; i++)
list[i] = null;
list.Clear();
list.TrimExcess();
list = null;
}

static void Main(string[] args)
{
SciTech.NetMemProfiler.MemSnapShot memSnapshot0 = SciTech.NetMemProfiler.MemProfiler.FullSnapShot();
TestArray();
SciTech.NetMemProfiler.MemSnapShot memSnapshot1 = SciTech.NetMemProfiler.MemProfiler.FullSnapShot();
TestList();
SciTech.NetMemProfiler.MemSnapShot memSnapshot2 = SciTech.NetMemProfiler.MemProfiler.FullSnapShot();
}
}
}
scot.prichard@med.ge.com
 
Posts: 3
Joined: Wed Feb 01, 2006 10:43 pm

Postby Andreas Suurkuusk » Mon Mar 13, 2006 9:49 pm

When you create the List<Leak> instance in the TestList method, the class constructor creates an empty Leak array and stores it in a static field (List<T>._emptyArray). The empty array is used by the default constructor and when setting Capacity to 0. So this is not a leak; if you run TestList more times, no more Leak arrays will be created.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 963
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden


Return to Future Improvements

Who is online

Users browsing this forum: No registered users and 2 guests

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.


SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla
Sweden


E-mail: mail@scitech.se

Telephone: +46-706868081

cron