如何实现只有在空闲内存不足时才将数据交换到磁盘的写入缓存 [英] How to implement a write cache that swaps data to disk only when free memory is low

查看:211
本文介绍了如何实现只有在空闲内存不足时才将数据交换到磁盘的写入缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将应用程序产生的数据缓存在内存中,但是如果内存变得稀少,我想将数据交换到磁盘。



理想情况下,我想由虚拟机通知它需要内存并将我的数据写入磁盘并以这种方式释放一些内存。但是我看不出有什么办法可以在通知我 之前将自己关注到虚拟机中,而 OutOfMemoryError 发生在某处(最有可能出现在代码中不以任何方式与高速缓存相关)。
$ b $ < java.lang.ref 中的引用类似乎不是在这种情况下有任何用处,它们的通知机制( ReferenceQueue )仅在GC已回收引用后触发。那么将数据保存到磁盘就太迟了。

有效管理堆内存的方法有哪些? (直到完全无法避免时才切换到磁盘)






Edit1 :回应评论操作系统已经为你做了这件事 - 这只涵盖了部分问题 - 操作系统可以分配的内存量是一种有限的资源。除此之外,还有其他一些限制,比如操作系统可用的内存数量,这里需要考虑:


  • 虚拟机体系结构( 32位虚拟机

  • 可以分配给虚拟机进程的内存限制( 32
  • 使用 -Xmx 选项
  • 可能强加给VM的限制 >


即使操作系统仍然有足够的可用空间,运行无限堆大小的虚拟机也不能防止内存溢出,我们建议您使用一些API调用来监控可用的可用内存并采取相应的行动。



请参阅这个问题关于如何监视可用于JVM的可用内存量。


I want to cache data produced by my application in memory, but if memory becomes scarce I would like to swap the data to disk.

Ideally I would want to be notified by the VM that it needs memory and write my data to disk and free some memory that way. But I don't see any way to hook myself into the VM in a manner that notifies me before an OutOfMemoryError occurs somewhere (most likely in code not related to the cache in any way).

The Reference classes in java.lang.ref do not seem to be of any use in this case, their notification mechanism (ReferenceQueue) only triggers after the reference has already been reclaimed by the GC. Then it would be too late to save the data to disk.

What alternatives are available to manage the heap memory efficiently? (do not swap to disk until absolutely unavoidable)


Edit1: In response to the comment "The OS already does that for you" - this only covers part of the issue - the amount of memory the OS can allocate is a limited resource. There are also other limits than the amount of memory available to the OS that need to be considered here:

  • The limit imposed by the VM's architecture (32-Bit VM)
  • The limit of memory that can be allocated to the VM's process (32-Bit OS)
  • The limit possibly imposed on the VM using the -Xmx option

Simply running the VM with unlimited heap size will not prevent it from running out of memory, even if the OS still has plenty available it may not be available to the VM for above reasons.

解决方案

I recommend you use some API calls to monitor the free memory available and act accordingly.

See this question about how to monitor the amount of free memory available to the JVM.

这篇关于如何实现只有在空闲内存不足时才将数据交换到磁盘的写入缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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