C# 弱参考收益
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/310685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Weak reference benefits
提问by leora
Can someone explain the main benefits of different types of references in C#?
有人可以解释一下 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.
我们有一个消耗大量内存的应用程序,我们正在尝试确定这是否是一个需要关注的领域。
采纳答案by Scott Pedersen
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.
我相信软引用和幻像引用来自 Java。长弱引用(将 true 传递给 C# 的 WeakReference 构造函数)可能被认为类似于 Java 的 PhantomReference。如果 C# 中有一个类似于 SoftReference 的,我不知道它是什么。
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.
这是否有助于减少应用程序的内存消耗将完全取决于应用程序的具体情况。例如,如果您有适量的缓存对象,这些对象将来可能会或可能不会被重用,那么弱引用可以帮助改善缓存的内存消耗。但是,如果应用程序正在处理大量小对象,弱引用会使问题变得更糟,因为引用对象将占用尽可能多或更多的内存。
回答by MusiGenesis
MSDN has a good explanation of weak references. The key quote is at the bottom where it says:
MSDN 对弱引用有很好的解释。关键引用在底部,它说:
Avoid using weak references as anautomatic solution to memorymanagement 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.
每次我在野外看到 WeakReference 时,它都被用作内存管理问题的自动解决方案。您的应用程序的问题可能有更好的解决方案。
回答by Artru
Brilliant real example with WeakReference is explained in Android development tutorial.
Android 开发教程中解释了使用 WeakReference 的精彩真实示例。
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.
视图 (ImageView) 上有一个图像 (Bitmap) 和图像容器。如果图像不是从内存加载(而是从磁盘、网络),那么它可以锁定 UI 线程和屏幕。为了避免它,可以使用异步任务。
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.
异步任务完成时会出现问题。那时图像容器可能根本没有用(屏幕发生变化或滚动后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);
}
}
}
}
P.S. the example is in Java, but can be understood by C# developers.
Source: http://developersdev.blogspot.ru/2014/01/weakreference-example.html
PS 这个例子是用 Java 编写的,但 C# 开发人员可以理解。
来源:http: //developersdev.blogspot.ru/2014/01/weakreference-example.html