1

I have a List of objects, which can be accessed by multiple users from a WebService. However, the number of objects in the list is steadily growing, so I need some memory management. I would like to clear all elements from the list, which are not used by any user. However, I cannot do this simply by calling the GC, because there is still one reference (the one from the List). And I don't know, how to get the number of references to an object.

So, is there a way, how to clear all objects, that have just one reference? Or get the number of references? Or determine, whether there is no other reference outside the List? Any solution is welcome.

Storm
  • 3,062
  • 4
  • 23
  • 54
  • If you are trying to remove an item from a list, it would be `yourList.Remove(theItem)`. Is that what you are searching for? – gunr2171 Dec 13 '13 at 21:06
  • 1
    Any reason why you are not using any of the framework caching solutions? To paraphrase Raymond Chen, a cache with a bad retention policy is just another name for a memory leak. – asawyer Dec 13 '13 at 21:07
  • If you're returning it as a response to a web request, then the item isn't being used in the entire application domain at all; it is being used by an entirely different process, if not an entirely different physical machine. – Servy Dec 13 '13 at 21:07
  • 4
    Why don't you use other data structures that fits better to your needs. Cache? HashSet? Dictionary? Seems like [XY-problem](http://www.perlmonks.org/?node_id=542341) – L.B Dec 13 '13 at 21:07
  • How about swapping the lists in threadsafe way:Interlocked.Exchange(ref currentList, newList); – user3096476 Dec 13 '13 at 21:08
  • It is a school project for implementing various design patterns, and I have Identity map http://en.wikipedia.org/wiki/Identity_map_pattern I did not use cache, because I have not known about them and I am unfamiliar with their concept. I did not use HashSet, because I use indices for getting some elements. And I don't see any reason, why should I use a Dictionary, since I am storing just a single value, not a pair. – Storm Dec 13 '13 at 21:20

1 Answers1

2

You can use a so called Weak List.

Basically a weak list is a list whose references are "ignored" by the GC. So while there is still a reference from the list, it will not be counted and (depending on which implementation of weak list you use) the item will be removed automatically at one point from the list.

Unfortunately there is no direct implementation of a weak list in the .NET Framework. There is the ConditionalWeakTable though which you might be able to use like a list and there are several examples for weak lists on the web which use the WeakReference type or similar mechanisms.

Examples:

Is there a way to do a WeakList or WeakCollection (like WeakReference) in CLR?

Community
  • 1
  • 1
aKzenT
  • 7,775
  • 2
  • 36
  • 65
  • A friend recommended creating a List>, storing WeakReference(myObject, true) ...would it do the work? – Storm Dec 14 '13 at 17:39
  • 1
    Not completely. While this would clean up the object values, but you still have a small memory leak from the WeakReferenceEntries themself. So you would need to periodically check whether some of the WeakReference entries are dead and remove them from the list. Some of the solutions you find on the web do exactly this ("prunning") – aKzenT Dec 15 '13 at 12:19