3

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.

Domchi
  • 10,705
  • 6
  • 54
  • 64
  • Perhaps you want to tell the operating system that your application should have a high priority when it comes to deciding what application get swapped or pages when resources are low. Search for [swappiness](http://superuser.com/questions/644136/modify-windows-swappiness). What about adjusting the pid priority in taskmanager? Perhaps there are other Hotspot options such as XX:LargePageSizeInBytes. – javajon Jul 09 '14 at 02:32
  • With option 2. did you raise the thread priority and perhaps setDeamon()? – javajon Jul 09 '14 at 02:38
  • not possible, only Native OS can to determine what, when, how long, direct way to see blue screen – mKorbel Jul 09 '14 at 07:27
  • @javajon - I'm using TimerTask which has normal priority for option 2., and Swing event dispatch thread which updates the GUI is at a slightly higher priority by default. I've played a bit with thread priority but haven't found that it affects what gets swapped. Swapiness is cool, didn't know about it, but doesn't seem to be able to affect a single application. There is a bit about locking pages in this SO question for Windows, but it looks very hacky and I'd have to drop to JNI: http://superuser.com/questions/237137/how-to-control-ram-swap-usage – Domchi Jul 13 '14 at 02:16
  • Have you proven the cause is the OS swap file? Did you try disabling the swapfile at the OS level just to verify that is really what is going on? See your same question [here](http://superuser.com/questions/512494/how-to-keep-program-always-in-memory-no-swapping). – javajon Jul 13 '14 at 06:53
  • Write a second program that somehow periodically nudges your application before is sleeps, via awt.Robot? In OS X there is a "prevent app nap" setting for an individual program. – javajon Jul 13 '14 at 07:09
  • @javajon - I've confirmed that it's definitely due to app being moved to swap, when swap is off, it shows up immediately. Robot is an interesting idea which might work, I'll try that. – Domchi Jul 14 '14 at 23:58

1 Answers1

1

I'm certain not all OSes supports that, but it looks like it's the only way - mlockall-agent

mlockall-agent.jar is a Java Agent that can be used in conjunction with any java application to cause the virtual memory used by the application to be "locked" in memory -- preventing it from being swapped to disk.

evgenii
  • 1,190
  • 1
  • 8
  • 21