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(S essions);
SessionData session=new SessionData();
Sessions.Add(iN extSessionID,se ssion);
iSessionID=iNex tSessionID;
iNextSessionID+ +;
Monitor.Exit(Se ssions);
return iNextSessionID;
}
public static void RemoveSession(i nt iSessionID)
{
Monitor.Enter(S essions);
SessionData s=(SessionData) Sessions[iSessionID];
s.Ports.Clear() ;
Sessions.Remove (iSessionID);
Monitor.Exit(Se ssions);
}
}
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. 1 2825
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.co m
"Chris Bardon" <cm******@engma il.uwaterloo.ca > wrote in message
news:5c******** *************** *@posting.googl e.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(S essions); SessionData session=new SessionData(); Sessions.Add(iN extSessionID,se ssion); iSessionID=iNex tSessionID; iNextSessionID+ +; Monitor.Exit(Se ssions); return iNextSessionID; } public static void RemoveSession(i nt iSessionID) { Monitor.Enter(S essions); SessionData s=(SessionData) Sessions[iSessionID]; s.Ports.Clear() ; Sessions.Remove (iSessionID); Monitor.Exit(Se ssions); } }
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
template <typename T>
class SortedList
{
public:
void insert( T item); // Write this
|
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 access it for a few hours? Other posts
to this board seem to point to no.
Thanks in advance.
Adam Smith
|
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 key-value pairs
of UserID - DisplayName for a whole bunch of user objects. The UserID
is simply a long, the DisplayName is something like, "Jones, Bill". I
want the SortedList to sort the names alphabetically, rather than by
|
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, System.EventArgs e)
{
GlobalCounter.Increment();
|
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
Static member 'ClassInCS.ABC.MyInt' cannot be accessed
with an instance reference; qualify it with a type name
| |
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 items might be garbage collected
if they're not used for a period of time?
For example:
public static class test
{
static Collection<string> coll;
|
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 SortedList like this:
class OtherSortedList : SortedList
{
.....
int GetTotalAmountOfAllItems()....
}
|
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:
System::Collections::SortedList,
System::Collections::Generic::SortedList,
using namespace System::Collections;
using namespace System::Collections::Generic;
Below is a working version of the generic template SortedList, which
|
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 area and look for probable
pointers into the heap.
I am stuck in the implementation part, where I am not very sure on
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |