弱引用的好处

发布于 2024-07-09 10:20:04 字数 149 浏览 7 评论 0原文

有人能解释一下 C# 中不同类型引用的主要好处吗?

  • 弱引用
  • 软引用
  • 虚引用
  • 强引用。

我们有一个消耗大量内存的应用程序,我们正在尝试确定这是否是一个需要关注的领域。

Can someone explain the main benefits of different types of references in C#?

  • Weak references
  • Soft references
  • Phantom references
  • Strong references.

We have an application that is consuming a lot of memory and we are trying to determine if this is an area to focus on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2024-07-16 10:20:04

我相信软引用和幻像引用来自 Java。 长弱引用(将 true 传递给 C# 的 WeakReference 构造函数)可能被认为与 Java 的 PhantomReference 类似。 如果 C# 中有 SoftReference 的类似物,我不知道它是什么。

弱引用不会延长对象的生命周期,因此一旦所有强引用超出范围,就可以对其进行垃圾回收。 它们对于保存初始化成本昂贵的大型对象很有用,但如果它们没有被积极使用,则应该可用于垃圾回收。

这是否有助于减少应用程序的内存消耗完全取决于应用程序的具体情况。 例如,如果您有一定数量的缓存对象,将来可能会或可能不会重用,则弱引用可以帮助改善缓存的内存消耗。 但是,如果应用程序正在处理大量小对象,弱引用将使问题变得更糟,因为引用对象将占用同样多或更多的内存。

Soft and phantom references come from Java, I believe. A long weak reference (pass true to C#'s WeakReference constructor) might be considered similar to Java's PhantomReference. If there is an analog to SoftReference in C#, I don't know what it is.

Weak references do not extend the lifespan of an object, thus allowing it to be garbage collected once all strong references have gone out of scope. They can be useful for holding on to large objects that are expensive to initialize, but should be available for garbage collection if they are not actively in use.

Whether or not this will be useful in reducing the memory consumption of your application will depend completely on the specifics of the application. For example, if you have a moderate number of cached objects hanging around that may or may not be reused in the future, weak references could help improve the memory consumption of the caches. However, if the app is working with a very large number of small objects, weak references will make the problem worse since the reference objects will take up as much or more memory.

沧桑㈠ 2024-07-16 10:20:04

MSDN 对弱引用有很好的解释。 关键引用位于底部:

避免使用弱引用作为
自动解决内存问题
管理问题。 相反,开发
有效的缓存策略
处理应用程序的对象。

每次我在野外看到 WeakReference 时,它​​都被用作内存管理问题的自动解决方案。 对于您的应用程序问题,可能有更好的解决方案。

MSDN has a good explanation of weak references. The key quote is at the bottom where it says:

Avoid using weak references as an
automatic solution to memory
management problems. Instead, develop
an effective caching policy for
handling your application's objects.

Every time I've seen a WeakReference in the wild, it's been used as an automatic solution to memory management problems. There are likely better solutions to your application's problems.

再可℃爱ぅ一点好了 2024-07-16 10:20:04

Android 开发教程中解释了使用 Wea​​kReference 的精彩真实示例。

视图上有图像(Bitmap)和图像容器(ImageView)。 如果图像不是从内存加载(而是从磁盘、网络),那么它可以锁定 UI 线程和屏幕。 为了避免这种情况,可以使用异步任务。

当异步任务完成时就会出现问题。 那时图像容器可能根本没有用(屏幕发生变化或 Android 在滚动后卸载不可见的视图部分)。 WeakReference 可以在这里提供帮助,并且 ImageView 将被垃圾收集。

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }
    // Method for getting bitmap is removed for code clearness

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

PS 该示例是用 Java 编写的,但 C# 开发人员可以理解。
资料来源: http://developersdev.blogspot.ru/2014/01/weakreference-示例.html

Brilliant real example with WeakReference is explained in Android development tutorial.

There is an image (Bitmap) and image container on the view (ImageView). If image will be loaded not from memory (but e.g. from disk, net) then it can lock UI thread and the screen. To avoid it an async task can be used.

The problem arises when async task finishes. Image container can be not useful at all at that time (screen is changed or Android unloads invisible view part after scrolling). WeakReference can help here and ImageView will be garbage collected.

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }
    // Method for getting bitmap is removed for code clearness

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

P.S. the example is in Java, but can be understood by C# developers.
Source: http://developersdev.blogspot.ru/2014/01/weakreference-example.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文