473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Static SortedList and Garbage Collection

I'm working on a class library in .net, and am having a problem that I
can only describe as a memory leak (which garbage collection was
supposed to eliminate). My class library contains a single class, one
member of which is a static SortedList of session data objects. One
of my methods creates a new object on this list, and another removes
the item from the list, which should make it eligible for garbage
collection. I've tested this in a multithreaded setting though, with
new threads creating a new session, calling Sleep(), and then removing
the session. I can see the memory increase, but nothing ever seems to
decrease. My code looks something like this:

public class SessionData
{
public ArrayList Ports=new ArrayList();
}

public class ClassLib
{
public static SortedList Sessions=new SortedList();
private statid int iNextSessionID=0;

public static int CreateSession()
{
Monitor.Enter(Sessions);
SessionData session=new SessionData();
Sessions.Add(iNextSessionID,session);
iSessionID=iNextSessionID;
iNextSessionID++;
Monitor.Exit(Sessions);
return iNextSessionID;
}
public static void RemoveSession(int iSessionID)
{
Monitor.Enter(Sessions);
SessionData s=(SessionData)Sessions[iSessionID];
s.Ports.Clear();
Sessions.Remove(iSessionID);
Monitor.Exit(Sessions);
}
}

Is there something else that I have to do in my RemoveSession() method
besides removing the object from the SortedList to get it to be
collected by the GC? I've tested the garbage collection by calling
GC.Collect() in my test app.
Nov 16 '05 #1
1 2787
Chris,

You should not be using directo calls to Monitor.Enter and Monitor.Exit.
Rather, you should use the lock statement, as it will clean up correctly if
an exception is thrown.

Just because you don't see the memory decrease doesn't mean that your
object was not disposed of. Looking at the memory numbers is not the proper
way to determine if your object was GCed, because that number is a measure
of the working set, which is not related to what you are trying to find out.

Assuming that you are not setting the object to be referred to anywhere
else (and it looks like you are not), I would not assume that the GC is not
working correctly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Bardon" <cm******@engmail.uwaterloo.ca> wrote in message
news:5c************************@posting.google.com ...
I'm working on a class library in .net, and am having a problem that I
can only describe as a memory leak (which garbage collection was
supposed to eliminate). My class library contains a single class, one
member of which is a static SortedList of session data objects. One
of my methods creates a new object on this list, and another removes
the item from the list, which should make it eligible for garbage
collection. I've tested this in a multithreaded setting though, with
new threads creating a new session, calling Sleep(), and then removing
the session. I can see the memory increase, but nothing ever seems to
decrease. My code looks something like this:

public class SessionData
{
public ArrayList Ports=new ArrayList();
}

public class ClassLib
{
public static SortedList Sessions=new SortedList();
private statid int iNextSessionID=0;

public static int CreateSession()
{
Monitor.Enter(Sessions);
SessionData session=new SessionData();
Sessions.Add(iNextSessionID,session);
iSessionID=iNextSessionID;
iNextSessionID++;
Monitor.Exit(Sessions);
return iNextSessionID;
}
public static void RemoveSession(int iSessionID)
{
Monitor.Enter(Sessions);
SessionData s=(SessionData)Sessions[iSessionID];
s.Ports.Clear();
Sessions.Remove(iSessionID);
Monitor.Exit(Sessions);
}
}

Is there something else that I have to do in my RemoveSession() method
besides removing the object from the SortedList to get it to be
collected by the GC? I've tested the garbage collection by calling
GC.Collect() in my test app.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ook | last post by:
What am I doing wrong? This code gives a compile error: 'SortedList<T>::insert' : illegal call of non-static member function. I've tried several variations of this, but keep getting the same error....
3
by: Adam Smith | last post by:
Hi. I have a static arraylist in a class which is populated during the class's static constructor. Will this arraylist or the class objects allocated on it ever be garbage collected if I don't...
1
by: gerrod | last post by:
Hi - Does anyone know a way to created a SortedList (in the System.Collections namespace) that will sort on VALUES instead of KEYS... ? The scenario is this - I have a SortedList containing...
4
by: Rick | last post by:
Hello, Below I create a static instance of an object, works well. public static BasicCounter GlobalCounter = new BasicCounter("GlobalCounter"); private void Page_Load(object sender,...
74
by: Mark | last post by:
Hello Friends Please check the code below. One in C# and other in VB .Net In C# I am not able to access a static property by an instance variable, but in VB I can do it easily. The Error is ...
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
3
by: R Karthick | last post by:
Hi, I am in the process of designing a conservative garbage collector for C ++. As one of the requirement of conservative garbage collection, I will have to scan through the static memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.