I'm writing a Java program that stores sensitive data (password and private keys) in memory. It will be deployed freely to any OS. I know that a user can create a memory dump manually on almost any system, but I am worried about a dump being created by the OS or JVM implementation (including, but not limited to some segfault of the JVM itself) that would compromise the privacy of the sensitive data.
Are there any steps that could be taken to reduce these risks? This question is POSIX specific but gives me an answer for these platforms. I had one non-platform specific idea that included setting an UncaughtExceptionHandler
(like this) to a class that would overwrite sensitive data. But what about if memory is swapped out? What if the JVM crashes (e.g. segmentation fault) due to a JVM/JNI bug? I know Linux can stop data from being swapped to disk but is there a Java code to do this cross-platform? Mostly I'm worried about the potential for recovery of data on magnetic storage devices so any help is appreciated.