RemoveAt(x);它是否放置元素x? [英] RemoveAt(x); Does it dispose the element x?

查看:40
本文介绍了RemoveAt(x);它是否放置元素x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cache,每次用户单击下一个图像时都存储最近的两个图像,"RemoveAt(x)"是否放置了x图像,或者我要删除的图像不在内存中.完全删除.

I have a Cache to store the most two recent images every time the user clicks the next image, Does the "RemoveAt(x)" dispose the x image, or what I want is for the removed image not to be in memory. to be totally removed.

 List<Image> BackimageList = new List<Image>();

 private void BackimageListCache(Image img)
{
  BackimageList.Add(img);
  if (BackimageList.Count > 2) 
   {
    BackimageList.RemoveAt(0); //oldest image has index 0
   }
}

推荐答案

.NET中的集合不拥有"一个对象.因此,他们不能假定该对象没有在其他任何地方使用,因此不能处置该对象.所有权规则完全由您自己执行.这确实意味着您还必须确保图片没有显示在PictureBox中.

Collections in .NET do not "own" an object. So they cannot assume that the object isn't used anywhere else, they can thus not dispose the object. Ownership rules are entirely your own to implement. Which does imply that you also have to be sure the the image isn't, say, displayed in a PictureBox.

确保图像不再占用任何内存也很困难.您无法自己管理内存,这是垃圾收集器的工作.但是,Image使用相当多的非托管内存来存储像素数据,当您调用Dispose()时,该内存确实会释放. Image的受管部分将保留在内存中,直到GC到达它为止.它很小.

Ensuring the image doesn't occupy any memory anymore is iffy as well. You cannot manage memory yourself, it is the job of the garbage collector. However, Image uses a rather substantial amount of unmanaged memory to store the pixel data, that memory does get released when you call Dispose(). The managed part of Image stays in memory until the GC gets to it. It is small.

这篇关于RemoveAt(x);它是否放置元素x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆