告诉 Java 不要将对象推入交换空间
是否可以告诉 JVM 提示操作系统最好不要将某个对象推出交换空间?
Is it possible tell the JVM to hint the OS that a certain object should preferably not get pushed out to swap space?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最简洁的答案是不。
Java 不允许您对换入的内容以及“固定”到 RAM 中的内容进行任何控制。
担心这类事情通常表明您的项目中还存在其他问题。 总体而言,操作系统会更好地确定哪些内容应该交换,哪些内容不应该交换。 您的工作是编写软件,这样它就不会尝试再次猜测底层虚拟机/操作系统将要做什么,而只需专注于提供您的功能和良好的设计。
The short answer is no.
Java doesn't allow you any control over what is swapped in and what is 'pinned' into RAM.
Worrying about this sort of thing is usually a sign that there is something else wrong in your project. The OS will on the whole do a much better job of working out what should be swapped and what shouldn't. Your job is to write your software such that it doesn't try to second guess what underlying VM/OS is going to do, just concentrate on delivering your features and a good design.
这个问题在 Eclipse 和 KeepResident dirty hack 插件 (http://suif.stanford .edu/pub/keepresident/)避免了它。
这可能是一个好的起点? 我还没有看到它被广泛使用,所以也许它已经集成在标准 Eclipse 发行版中?
This problem has also been very noticeable in Eclipse and the KeepResident dirty hack plugin (http://suif.stanford.edu/pub/keepresident/) avoids it.
It might be a good place to start? I have not seen it in widespread use so perhaps this has been integrated in the standard Eclipse distribution?
嘿! 您正在使用托管语言进行编程。 你为什么想这些? 如果你无法忘记这些东西,你总是可以选择用 C 编程。
Hey! You are programming in a managed language. Why are you thinking about these? If you can't get these stuff out of your mind, you can always choose to program in C.
简短的答案是(如上所述):不要这样做:-)。
然而原则上这是可能的。 大多数操作系统确实允许“锁定”某些内存区域以防止交换(例如 mlock(2) Linux 下,Windows 下 VirtualLock)。
VM 可以通过合适的 API 向 Java 应用程序公开此功能。 然而,我所知道的虚拟机都没有这样做,所以要做到这一点,你首先必须修改你的虚拟机......
The short answer is (as given above): Dont' do it :-).
It would however be possible in principle. Most OS do allow to "lock" certain memory areas from being swapped (e.g. mlock(2) under Linux, VirtualLock under Windows).
The VM could expose this functionality to Java applications via a suitable API. However, no VM I know of does that, so to do it, you would first have to modify your VM...
如果您定期访问它,那么无论它当时所处的页面(JVM 在垃圾收集期间移动内容)都不会被调出,除非其他东西更积极地请求内存。 但是没有办法告诉 JVM 不要将其移动到另一个页面,并且操作系统只知道页面。
If you access it regularly, that whatever page it happens to be in at the time (the JVM moves stuff around during garbage collection) will not be paged out unless something else is requesting memory even more aggressively. But there is no way of telling the JVM to not move it to another page, and the OS only knows about pages.
不是答案,但缺乏评论点,我保留此选项:)
有理由不将信息存储在交换中。 无论是密码还是其他机密信息,都不应该在磁盘上永久保存。 另外,周末后回到我的电脑时,我希望内存中的一些东西可以立即可用。
(非 Java)对于每个/大多数操作系统来说,本机可能有某种方法可以做到这一点。 对于 Windows,这绝对是可能的。 但不是直接来自java(想想JNI)。
根据此选项的绝望程度,您始终可以考虑使用视频内存或其他一些不会换出的硬件设备。 这允许您仍然使用标准的 java api,例如 jogl 来存储信息。 但不知何故,我怀疑这是否与您正在寻找的实现/结果相符。
Not an answer, but lacking points to comment, I reserve this option :)
There are reasons to not store information in swap. Be it passwords or other confidential information that should not spend eternity on disk. Also, coming back after a weekend to my pc, I'd like some things to be in memory immediately available.
(Non Java) Natively there is probably some way to do this for each/most operating systems. With windows this is definitely possible. But not straight out of java (think JNI).
Depending on how desperate this option is, you could always look at using video memory, or some other hardware device that does not swap out. This allows you to still use a standardish java api, like jogl to store information. But somehow I doubt that is in context with the implementation/results you are looking for.
基本上,您希望始终将整个 JVM 保留在主内存中。
Basically you want to keep the whole JVM in main memory the whole time.