Helping Garbage collection

jhp3jhp3 GBMember

The documentation gives the following example of reducing pressure on the garbage collector, by avoiding having a static List in a Peer class.
My question is, is there anything special about the syntax of the recommended HiddenReference class, or does the following have the same effect?

class BetterActivity : Activity {

private StoreData _storeData = new StoreData();

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);



    _storeData.Strings.Value = new List<string> (
            Enumerable.Range (0, 10000)
            .Select(v => new string ('x', v % 1000)));
}

}

class StoreData
{
public StoreData();

    public List<string> Strings;

}

Documentation Bad example

class BadActivity : Activity {

private List<string> strings;

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    strings.Value = new List<string> (
            Enumerable.Range (0, 10000)
            .Select(v => new string ('x', v % 1000)));
}

}

Documentation Good example

class BetterActivity : Activity {

HiddenReference<List<string>> strings = new HiddenReference<List<string>>();

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    strings.Value = new List<string> (
            Enumerable.Range (0, 10000)
            .Select(v => new string ('x', v % 1000)));
}

}

class HiddenReference {

static Dictionary<int, T> table = new Dictionary<int, T> ();
static int idgen = 0;

int id;

public HiddenReference ()
{
    lock (table) {
        id = idgen ++;
    }
}

~HiddenReference ()
{
    lock (table) {
        table.Remove (id);
    }
}

public T Value {
    get { lock (table) { return table [id]; } }
    set { lock (table) { table [id] = value; } }
}

}

Sign In or Register to comment.