views:

502

answers:

7

Is it possible tell the JVM to hint the OS that a certain object should preferably not get pushed out to swap space?

+2  A: 

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.

Mehrdad Afshari
The other option, I guess, would be not to force the application to keep everything in memory at once. The question comes more from curiosity than actual need. Is it possible to do in C?
Sebastian Ganslandt
It is possible to stick some memory pages in memory. This is what most VM environments do.
Mehrdad Afshari
+10  A: 

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.

Gareth Davis
+1  A: 

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...

sleske
A: 

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.

Pete Kirkham
+3  A: 

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?

Thorbjørn Ravn Andersen
+1  A: 

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.

A: 

Basically you want to keep the whole JVM in main memory the whole time.

Peter Lawrey