大对象缓存
我有一个 .NET2.0 C# Web 应用程序。 它具有数量可变的大型、初始化昂贵的对象,这些对象在多个请求之间共享,但不与任何给定用户进行会话。 因此,我需要将它们保留在查找结构中。 这些对象需要根据需要创建,并且在应用程序的生命周期中不需要,而只是在其使用的生命周期中需要。
加一点。
执行此操作的内存泄漏方法是一个简单的字典,执行此操作的内存安全方法是一个弱引用支持的字典,但我遇到的问题是 GC 太快了。 实际上,这可能不是问题,因为对象的流量应该使它们能够保持活动状态,而不会被迫重新生成太多,但理想情况下,我希望它们也能缩小规模。
是否有某种我没有想到的中间解决方案,可以使对象安全地隐藏在GC一段时间内X,但也允许它们在该时间结束时被收集,最好是在那个时间的地方计数器每次以类似于会话令牌的方式使用时都会重置?
I have a .NET2.0 C# web-app. It has a variable number of large, init-expensive objects which are shared across multiple requests but not sessioned to any given user. I therefore need to persist them in a lookup structure. These objects need to be created as required and are not required for the lifespan of the app, merely the lifespan of their usage.
Plus a bit.
The memory leaking way to do this is a simple dictionary, the memory safe way to do this is a weakreference backed dictionary, but the problem I'm having is that the GC is just too damned fast. Realistically this may not be a problem because the traffic to the objects should be such that they'll stay alive without being forced to regenerate too much, but ideally I'd like them to scale down too.
Is there some kind of middle-ground solution I'm not thinking of which will keep the objects safely hidden from the GC for a period of time X, but also allow them to be collected at the end of that time, preferably where that time counter gets reset every time they're used in a way similar to session tokens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定为什么 HttpRuntime 缓存 在这里不起作用。 插入到缓存中的项目每次被触摸时都会被“更新”,从而使它们保持活动状态,直到不再需要它们为止; 之后它们将继续保留在缓存中,直到过期(滑动或滚动时间)或者由于内存压力而被迫退出。 它们还可以在明确设置的绝对时间强制退出,无论使用情况如何:
绝对时间:系统时钟经过特定日期时间后强制退出项目
滑动(滚动)时间:每次触摸物品时,都会重置其死亡倒计时。 倒计时的持续时间就是滑动时间(例如5分钟)。
用法示例:
I'm not sure why the HttpRuntime cache would not work here. Items inserted into the cache will be "renewed" every time they are touched, thus keeping them alive until they are no longer needed; and after that they will continue to stay in cache until they expire (sliding or rolling time) or they are forced out due to memory pressure. They can also be forced out at an explicitly-set, absolute time regardless of usage:
Absolute time: items are forced out after the system clock passes a certain DateTime
Sliding (rolling) time: every time an item is touched, its countdown to death is reset. The duration of the countdown is the sliding time (for example, 5 minutes).
Example usage:
为什么不使用框架中的 Cache 对象并为其设置滑动过期时间。 说10分钟。 如果它至少每 10 分钟使用一次,它将保留在缓存中,如果 10 分钟过去,它将过期,GC 将删除它。
一个例子:(我相信这是完整的语法
Why don't you use the Cache object from the framework and set a sliding expiration on it. Say 10 minutes. If it is used at least once every 10 minutes it will stay in cache, if the 10 minutes elapses it will expire and GC will remove it.
An example: (I believe this is the full syntax
听起来您已经在描述自己问题的解决方案了; 一种缓存结构,它在一段时间内保留强引用,但如果一段时间后对象没有被“访问”,则将它们降级为弱引用。
It sounds like you're already describing a solution to your own problem; a kind of cache structure which holds strong references for some period of time but then downgrades them to weak references if some time passes without the object being 'accessed'.
关键部分是“它们的使用期限”。 你如何定义它? 如果你能以一种不需要了解未来的方式精确地定义它,那么剩下的事情可能就很容易了。
The key part is "the lifespan of their usage." How do you define that? If you can define it precisely in a way which doesn't require knowledge of the future, the rest is probably easy.