1
我讀的Android垃圾收集文檔在Garbage Collection -- Reduce Referenced Instances,我不是很明白這個代碼(Xamarin,Android)此代碼如何幫助減少引用實例?
class HiddenReference<T> {
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; } }
}
}
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)));
}
}
如何進行HiddenReference工作的機制?如果GC將遞歸地掃描BetterActivity引用的實例,它不能看到字符串字段中的列表,然後是列表中的所有字符串?我想我錯過了一些東西。任何幫助表示讚賞。
謝謝!
謝謝!非常明確的答案。所以這個策略不僅僅是Xamarin,它可以在其他一些.Net項目中使用嗎?像WinForm,WPF? – Student222
這是Xamarin.Android。我們必須確保Java GC和Mono GC的合作愉快。參考步行是讓這兩個世界相互影響的結果。 – Krumelur
因此,在一般的.Net項目中,GC不會執行遞歸掃描? Mono爲什麼需要做一個參考步驟以便與Java GC一起愉快地工作?原諒我的無知,謝謝~~! – Student222