Java 多次读取属性文件会消耗大量内存吗?
我有一个读取属性文件的类。请看下面。 应用程序运行时多次调用readProperties()方法,这是否意味着这里存在内存问题Java 多次读取属性文件会消耗大量内存吗?,java,memory,properties,Java,Memory,Properties,我有一个读取属性文件的类。请看下面。 应用程序运行时多次调用readProperties()方法,这是否意味着这里存在内存问题 public class PropertyReader { private static Properties configKeyValuePairs = null; private static String configPropertiesFileName = "Config.properties";
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.
}
}
是的,可能存在很大的内存问题,这取决于是否有调用类持有对新创建的properties对象的引用 试着这样做:
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();
}
}
}
}
}
是的,可能存在很大的内存问题,这取决于是否有调用类持有对新创建的properties对象的引用 试着这样做:
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();
}
}
}
}
}
假设属性文件从未更改,则可以执行以下操作:
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
}
假设属性文件从未更改,则可以执行以下操作:
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
}
加载properties对象一次,并将其存储为类成员 我发现很难相信你会因此而出现记忆问题
如果您发现确实如此,那么您可以随时返回并重新考虑它,但不要过早地优化可能不存在的问题。加载properties对象一次,并将其存储为类成员 我发现很难相信你会因此而出现记忆问题
如果你发现你是这样做的,那么你总是可以重新考虑它,但不要过早地优化一个可能不存在的问题。对于投票关闭的人来说,也许你认为这是一个愚蠢的问题,但这是一个我不知道的问题。因此,如果你知道答案,请把它放在这里,我会投赞成票。很难说,这取决于你如何处理那些
属性实例。在您的代码段中,它们是私有的,无法将它们从PropertyReader类中取出-在这种情况下,一次调用readProperties()
将在内存中保存一个实例,多次调用将丢弃生成垃圾的旧实例,但不会消耗更多的“活动”对象空间。@fd,感谢您的回复。我忘了在PropertyReader中添加getter。添加getter和setter会使它更难分辨;如果获得实例的人保留了这些实例,那么除了以后通过调用readProperties
创建的任何额外实例外,它们还将消耗内存;这些实例将过时。如果调用方总是在获取属性后不久丢弃实例,那么内存中可能不会超过1或2个实例。无论如何,我都会仔细考虑属性
实例的生命周期以及它们的用途。getter意味着您不会让属性
对象本身返回给调用方,这意味着只有一个活动实例,并且没有内存问题。但是,如果线程安全对应用程序很重要,请注意线程安全;事实上,在调用readProperties
至少一次之前,请确保不要调用getter。对于投票关闭的人来说,也许你认为这是一个愚蠢的问题,但这是一个我不知道的问题。因此,如果你知道答案,请把它放在这里,我会投赞成票。很难说,这取决于你如何处理那些属性实例。在您的代码段中,它们是私有的,无法将它们从PropertyReader类中取出-在这种情况下,一次调用readProperties()
将在内存中保存一个实例,多次调用将丢弃生成垃圾的旧实例,但不会消耗更多的“活动”对象空间。@fd,感谢您的回复。我忘了在PropertyReader中添加getter。添加getter和setter会使它更难分辨;如果获得实例的人保留了这些实例,那么除了以后通过调用readProperties
创建的任何额外实例外,它们还将消耗内存;这些实例将过时。如果调用方总是在获取属性后不久丢弃实例,那么内存中可能不会超过1或2个实例。无论如何,我都会仔细考虑属性
实例的生命周期以及它们的用途。getter意味着您不会让属性
对象本身返回给调用方,这意味着只有一个活动实例,并且没有内存问题。但是,如果线程安全对应用程序很重要,请注意线程安全;事实上,在调用readProperties
至少一次之前,请确保不要调用getter。这不是最好的解决方案,也不是同步的,但它会让您走上正确的轨道。您应该始终在finally块中调用.close()。一次调用两个反模式(没有正确关闭文件,也没有同步)。不要在生产代码中使用。是的,没有那么热。现在编辑。这不是最好的解决方案,也不是同步的,但它让您走上了正确的轨道。您应该始终在finally块中使用.close()。同时使用两个反模式(没有正确关闭文件,也没有同步)。不要在生产代码中使用。是的,没有那么热。正在编辑。如下面的评论“但不要过早地优化可能不存在的问题”。顺便说一句,properties对象被加载了很多次,因此我不确定是否存在内存问题。如果您没有耗尽内存或使用无法适应的内存量(取决于规格),则不会出现内存问题。如果你这样做了,我很难相信这是几十个属性。就像下面的评论“但是不要过早地优化一个可能不存在的问题。”顺便说一句,properties对象被加载了很多次,所以这就是为什么我不确定是否有内存问题。如果你没有耗尽内存或使用无法适应的内存量(取决于规格)您没有内存问题。如果有,我很难相信这是几十个属性。是否有任何特定的原因导致否决?请帮助我改进我的ans