-1

I'm writing a small MemoryManager for my WPF application and reached the following problem.

What i do: I store a lot of instances as a WeakReference in a IList<WeakReference>. Later, when i want to free all memory, i want to destroy all alive objects in the list.

To do this, i try to get the reference to the object, like this:

foreach (WeakReference wr in references)
{
    if (wr.IsAlive == true)
    {
        if(wr.Target != null)
        {
            TypedReference tf = __makeref(wr.Target);
        }
    }
}

But i dont't know how to destroy tf. I tried to use __refval, but it does not work for me.

Sample:

InstanceDestructManager idm = new InstanceDestructManager();
IList<string> test = new List<string>();
test.Add("123");

idm.AddNullable<IList<string>>(ref test);
idm.Dispose();

// Should not be possible, because after idm.Dispose "test" should be null
test.Add("456");

General code for:

public static void Test(ref object pa)
{
    pa = null;
}

Maybe some one has an idea, thank you!

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • 3
    In .NET, you don't 'destroy' objects. You leave them lingering and let the GC do its job. Your best course is to destroy this 'small MemoryManager' now. – H H Dec 30 '14 at 13:25
  • @HenkHolterman ok, than i will search for a new solution. Because my application memory is increasing very bad and it seems that the GC has some problem with detecting closed Tabs. Or i need to Dispose every thing in the "Close-Tab" event. Thank you! – BendEg Dec 30 '14 at 13:28
  • Dispose() is about resources, not about memory. Make sure you understand the problem first. – H H Dec 30 '14 at 13:31
  • And how bad is bad? Too many people have wasted far too much time just because some number in Taskmanager was going up. – H H Dec 30 '14 at 13:32
  • 1
    Until OutOfMemory exceptions. The application often reach 2GB of ram. – BendEg Dec 30 '14 at 13:33
  • 1
    OK, that is a problem. Now find out why. Look up 'Memory Profiler'. We can't help you from this, afaik Tabs don't close. – H H Dec 30 '14 at 13:35

1 Answers1

0

Setting the WeakReference.Target to null will release the reference to it. There is no such thing as destroy memory in C#. The GC collect memory when there are no references to it. And even then it decides on its own when to free them. GC.Collect forces this. But this is not for production purposes unless you know what you're doing.

Sievajet
  • 3,443
  • 2
  • 18
  • 22
  • Your solution can't work, because you only set `Target` to null and not what target is referencing. – BendEg Dec 30 '14 at 13:16
  • The target property is a setter, so yes you can set wnat the target is referencing – Sievajet Dec 30 '14 at 13:18
  • @Sievajat, but i don't want to set what target is referencing, i want to set the already referenced object to null. I want to find a general solution fore the code, under my update: **General code for** – BendEg Dec 30 '14 at 13:20
  • @BendEg : You can't _set an object to null_. That just makes no sense. – H H Dec 30 '14 at 13:34
  • Maybe, or improve it. Right now it's asking for help on a wrong solution. – H H Dec 30 '14 at 13:36