2

So I have this Java Swing app that has a curious usage pattern: it's not used all the time, but when it's used, it's essential that it's available immediately. And when I say immediately, I mean that it has to be ready for user input as soon as user presses shortcut to bring it in foreground.

The problem is, Windows (and OS generally) tends to put applications which are idle in swap. And when application window comes into foreground, on a typical IDE-powered system, it takes a few seconds for JVM to get loaded to memory again, for Swing to redraw window etc. I'm trying to avoid this and somehow suggest to OS to keep application in memory.

I have tried the following:

  1. Playing with -Xms JVM option. This doesn't really help with swap, although it does help somewhat, but it's hard to measure and inconsistent (in other words, fails in most cases).
  2. Running a task within the app that does something at regular intervals (go through the data, redraw main JPanel, etc). This doesn't seem to help at all. But maybe I haven't thought of correct something.
  3. Turning off swap file completely. This does help, but is obviously not something that is recommended for all setups, and I can't really bother (or count on) users to do this.

I'd be grateful for any ideas. Application is multiplatform, but focus is on Windows, and I'm OK with doing different thing for different OS if needed. I'm willing to play nice with OS if at all possible but I'm also ready to play rough if needed. My thinking is that OS probably doesn't provide a way for application to communicate its preference, but there must be a way to modify application behavior so that OS figures out that application is not a good candidate to put in swap.

EDIT: I have had some progress on this issue, with option 2. I've created a TimerTask which fires off once an hour and basically brings JFrame to front, that is, executes jFrame.setVisible(true) and jFrame.toFront(). This indeed keeps main window responsive! However as soon as another part of application needs to be accessed there is that dreadful pause again. This makes sense as OS probably keeps in memory only pages that are accessed. There must be a way to access all pages in JVM without flashing application window in front of the user, without needing to know application specific objects (don't want to update this code every time application is modified), and without compiler deciding that the access operation is no-op and skipping it.

4

1 回答 1

1

我确定并非所有操作系统都支持这一点,但看起来这是唯一的方法 - mlockall-agent

mlockall-agent.jar 是一个 Java 代理,可以与任何 Java 应用程序结合使用,以使应用程序使用的虚拟内存“锁定”在内存中——防止它被交换到磁盘。

于 2016-08-10T00:23:57.673 回答