Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java util Preferences constantly accesses disk about every 30 secs

Our application is using the java.util.prefs.Preferences class to store a few user data fields. Here is the code snippet of our preferences class below. Storage of our preferences works fine, however we noticed that the preferences continues to make disk accesses about every 30 seconds or so. Is there a way to disable these background disk accesses in the Preferences class? (.userRootModeFile.root is changed about every 30 seconds)

public class NtapPreferences {

private static Preferences prefs = Preferences.userRoot(); //.systemRoot();

/** Prepends "NTAP.<applicationName>." to 'key' for system-wide uniqueness. */
private static String getKeyForApp ( String key ) {
    return "NTAP." + Application.getApplicationName() + "." + key;
}

/**
 * Returns the application preference value for 'key'.<br/>
 * <br/>
 * If 'key' is not a defined preference, then 'def' is returned.
 */
public static String get ( String key, String def ) {
    return prefs.get(getKeyForApp(key), def);
}

/** Sets the application preference value for 'key' as 'value'. */
public static void put ( String key, String value ) {
    //TODO how do we want to resolve failures to put data in the preferences?
    try {
        prefs.put(getKeyForApp(key), value);
        prefs.flush();
    } catch (NullPointerException e) {
        LOG.error(NtapPreferences.class, e);
    } catch (IllegalArgumentException e) {
        LOG.error(NtapPreferences.class, e);
    } catch (IllegalStateException e) {
        LOG.error(NtapPreferences.class, e);
    } catch (BackingStoreException e) {
        LOG.error(NtapPreferences.class, e);
    }
}

/** Removes the application preference value for 'key' if one is defined. */
public static void remove ( String key ) {
    prefs.remove(getKeyForApp(key));
}

}

like image 708
user2533069 Avatar asked Oct 04 '22 16:10

user2533069


1 Answers

This behaviour is controlled by a system property called "java.util.prefs.syncInterval". This gives the interval (in seconds) between synchronizations as an integer (int) valued string. You can't turn off syncing entirely, but you can set the interval to a very large value ... though there is an upper bound of (I / we think) 597 days, due to the way the code converts the interval to milliseconds.

Reference:

  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/prefs/FileSystemPreferences.java?av=f
like image 114
Stephen C Avatar answered Oct 12 '22 11:10

Stephen C