简体   繁体   English

多次读取属性文件会消耗大量内存吗?

[英]Does reading properties files multiple times consume lots memory?

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? 当应用程序运行时,多次调用readProperties()方法,这是否意味着这里存在内存问题?


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

    static void readProperties() throws 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();
                    }
                }
          }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM