从缓存中删除项目时的 Asp.Net 缓存和内存泄漏
因此,我尝试在我的 Asp.Net 应用程序中引入一些缓存。 用户的新数据以大数据集的形式从数据库返回。 每当用户请求数据时,我都会将此数据集插入到 HttpRunTime.Cache 中。 目前我将其缓存时间设置为 2-3 小时。 这些数据集非常大,我经常使用不同的键将它们放入缓存中。 我担心的是这样做会导致内存泄漏。 Asp.Net 会处理缓存中过多的数据并将其删除吗? 此外,当 Asp.Net 或我使用 Cache.Remove() 删除缓存项时,是仅删除对数据集的引用还是从内存中对数据集进行垃圾收集? 是否存在数据集可能从缓存中“删除”但仍存在于内存中从而产生性能问题的情况? 如果是这种情况,有没有办法明确地“垃圾收集”它们?
So I'm trying to introduce some caching into my Asp.Net application. New data for the user is returned from the DB in the form of large datasets. Whenever a user requests data I insert this dataset into the HttpRunTime.Cache
. At the moment I'm setting their caching time to 2-3 hours. And these are really large datasets and I put them in Cache quite frequently with different keys. What I'm worried about are the memory leakage implications of doing this. Would Asp.Net take care of excessive data in Cache and remove it? Also when a cached item is removed by Asp.Net or by me using Cache.Remove()
, is only the reference to the dataset removed or the dataset is also garbage collected from memory? Is there a scenario in which datasets might be 'removed' from cache but still existing in memory creating performance issues? Is there a way to explicitly 'garbage collect' them if this is the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
放入缓存中的项目不保证存在,如果内存不足,框架会清除缓存。 您可以指定优先级来指示应首先自动清除哪些项目。
至于是否会释放内存,只要这些项目受到管理,并且您自己没有在应用程序的其他地方保留引用,一旦缓存超时到期(或者您手动将其从应用程序中删除),垃圾收集器就会释放内存。缓存)。
当然,从缓存中删除并不能保证物理内存将被释放,因为这只在下次垃圾收集器运行时发生。
Items put in the cache are not guaranteed to be there, with the framework clearing the cache if it runs low on memory. You can specify a priority to indicate which items should be automatically cleared first.
As to if the memory will be freed, as long as the items are managed, and you are not keeping a reference yourself elsewhere in the application the garbage collector will free the memory once the cache timeout has expired (or you manually remove it from the cache).
Of course removing from the cache doesn't guarantee that the physical memory will be freed, because that only happens the next time the garbage collector runs.