开发者

Does reading properties files multiple times consume lots memory?

开发者 https://www.devze.com 2023-02-13 20:02 出处:网络
I have a class which reads a properties file. Please see below. The method readProperties() is called many times when the application is running, does that mean there is a memory issue here?

I have a class which reads a properties file. Please see below. The method readProperties() is called many times when the application is running, does that mean there is a memory issue here?


public class PropertyReader {
    private static Properties   configKeyValuePairs         = null;
    private static String       configPropertiesFileName    = "Config.properties";

    static void readProperties() throws开发者_JS百科 FileNotFoundException, IOException {    
        configKeyValuePairs = new Properties();
        InputStream input = ConfigReader.class
                .getResourceAsStream(configPropertiesFileName);

        configKeyValuePairs.load(input);

        input.close();
    }

   static String getUserName(){
       //return user name which is from the properties file.    
    }
}






Assuming your properties file never changes, you can do the following:

public class MyApplicationConfiguration {
    private static Properties   configKeyValuePairs         = new Properties();
    private static String       configPropertiesFileName    = "Config.properties";

    static {
        InputStream input = null;
        try {
            input = MyApplicationConfiguration.class
                .getResourceAsStream(configPropertiesFileName);

            configKeyValuePairs.load(input);

        } catch (IOException e) {
            // Deal with not being able to load config, could be a fatal error!
        } finally {
            if (input != null) {
                input.close();
            }
        }
    }

    public static String getUsername() {
        // ...
    }

    // Implement getters for other configuration key-value pairs
    // DO NOT let configKeyValuePairs be returned to anyone
}


Load the properties object once, and store it a class member.

I find it hard to believe that you will have memory issues because of it.

If you find out that you do, then you can always comeback and rethink it, but don't prematurely optimize a problem that probably doesn't exist.


Yes, there could be a very big memory problem, depending on whether or not there are calling classes that hold a reference to the newly created properties object.

Try something like this:

public class PropertyReader {
    private static       Properties   configKeyValuePairs         = null;
    private static final String       configPropertiesFileName    = "Config.properties";


    public static void readProperties() throws FileNotFoundException, IOException { 
        if(null == configKeyValuePairs){
            InputStream input;
            synchronized(PropertyReader.class){
                try{
                    configKeyValuePairs = new Properties();
                    input = PropertyReader.class
                        .getResourceAsStream(configPropertiesFileName);

                    configKeyValuePairs.load(input);
                }finally{
                    //this can still throw ioexception!
                    if(null != input){
                        input.close();
                    }
                }
          }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号