472,794 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 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 2728
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.