垃圾收集器在 WeakReference 中删除(收集)对象的优先级是多少?

What is the priority of deleting(collect) objects in WeakReference by garbage collector?

我是 c# 的新手,我正在学习垃圾收集器,我正在上薄弱的参考课

here在msdn中使用弱引用的例子

示例:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // Create the cache.
        int cacheSize = 50;
        Random r = new Random();
        Cache c = new Cache(cacheSize);

    string DataName = "";
    GC.Collect(0);

    // Randomly access objects in the cache.
    for (int i = 0; i < c.Count; i++) {
        int index = r.Next(c.Count);

        // Access the object by getting a property value.
        DataName = c[index].Name;
    }
    // Show results.
    double regenPercent = c.RegenerationCount/(double)c.Count;
    Console.WriteLine("Cache size: {0}, Regenerated: {1:P2}%", c.Count, 
    regenPercent);
    
}

public class Cache
{
// Dictionary to contain the cache.
static Dictionary<int, WeakReference> _cache;

// Track the number of times an object is regenerated.
int regenCount = 0;

public Cache(int count)
{
    _cache = new Dictionary<int, WeakReference>();

    // Add objects with a short weak reference to the cache.
   for (int i = 0; i < count; i++) {
        _cache.Add(i, new WeakReference(new Data(i), false));
    }
}

// Number of items in the cache.
public int Count
{
    get {  return _cache.Count; }
}

// Number of times an object needs to be regenerated.
public int RegenerationCount
{
    get { return regenCount; }
}

// Retrieve a data object from the cache.
public Data this[int index]
{
    get {
        Data d = _cache[index].Target as Data;
        if (d == null) {
            // If the object was reclaimed, generate a new one.
            Console.WriteLine("Regenerate object at {0}: Yes", index);
            d = new Data(index);
            _cache[index].Target = d;
            regenCount++;
        }
        else {
            // Object was obtained with the weak reference.
            Console.WriteLine("Regenerate object at {0}: No", index);
        }

        return d;
   }
}
}

// This class creates byte arrays to simulate data.
public class Data
{
private byte[] _data;
private string _name;

public Data(int size)
{
    _data = new byte[size * 1024];
    _name = size.ToString();
}

// Simple property.
public string Name
{
    get { return _name; }
}
}
 // Example of the last lines of the output:
 //
 // ...
 // Regenerate object at 36: Yes
 // Regenerate object at 8: Yes
 // Regenerate object at 21: Yes
  // Regenerate object at 4: Yes
 // Regenerate object at 38: No
  // Regenerate object at 7: Yes
 // Regenerate object at 2: Yes
 // Regenerate object at 43: Yes
  // Regenerate object at 38: No
  // Cache size: 50, Regenerated: 94%

垃圾收集器在 WeakReference 中删除(收集)对象的优先级是多少?

为什么 GC 选择缓存中的这个对象来移除 94% 而保留 6%

WeakReferences 允许引用对象被垃圾回收。垃圾收集器将收集它正在收集的那一代中的所有可收集对象。

我的猜测是,当调用 GC.Collect(0) 时,某些对象已提升为 gen 1/2。如果你用 GC.Collect(2) 替换它,我希望没有任何对象存活。

WeakReference is not typically a great idea for caching。我避免它,并在我想缓存某些东西时使用强引用。通常结合一些估计内存使用情况的方法来设置内存上限。现在内存通常很充足,因此减少内存使用的重要性有限。

还有memory pooling的相关概念,这可以用来减少使用大内存缓冲区时的第2代GC。

编辑:

稍微简化了您的示例:

    static void Main(string[] args)
    {
        for (int i = 0; i < 100; i++)
        {
            GC.TryStartNoGCRegion(10000000);
            var cache = new Cache(50);
            Console.Write("Objects alive Before: " + cache.CountObjectsAlive());
            GC.EndNoGCRegion();
            GC.Collect(2, GCCollectionMode.Forced);
            Console.WriteLine("\tAfter : " + cache.CountObjectsAlive());
        }
        Console.ReadKey();
    }

public class Cache
{
    // Dictionary to contain the cache.
    Dictionary<int, WeakReference> _cache = new Dictionary<int, WeakReference>();

    public Cache(int count)
    {
        // Add objects with a short weak reference to the cache.
        for (int i = 0; i < count; i++)
        {
            _cache.Add(i, new WeakReference(new byte[i * 1024], false));
        }
    }
    public int CountObjectsAlive() => _cache.Values.Count(v => v.Target != null);
}

这始终在 GC 之前提供 50 个活动对象,在 GC 之后提供 0 个活动对象。请注意 TryStartNoGCRegion 在创建对象时防止 GC 运行 的用法。如果我更改程序以确保将某些对象提升到 gen 1/2 并且仅收集 gen 0,我会得到一些幸存的对象。

所以我想说我的观点仍然成立。 GC 将收集它收集的代中的所有对象。除非你有一些特定的用例,否则你最好不要乱用 Wea​​kReferences。