具有文件的对象的内存管理。
我想知道是否有人可以解释在以下情况下具有文件的对象的最小内存占用的最佳解决方案...
- 可能有 1 到几百个 Foo 类。
- 线程安全将来会很重要。
- 并非每次都会访问每个 Foo 类的文件。
- 每个文件都是唯一的。
- Foo 类中的文件可能会被多次访问。
我计划分析下面的解决方案,以找到最低的内存占用,我很清楚哪一个最有效,但我对一些反馈感兴趣。解决方案 1 似乎是最好的方法,但感觉访问 getter 的东西越多,就越容易出现内存泄漏。想法?
解决方案 1:
public class Foo{
private final String pathToFile;
public class Foo(String pathToFile){
this.pathToFile = pathToFile;
}
public File getFile(){
return new File(pathToFile);
}
}
解决方案 2:
public class Foo{
private final File file;
public class Foo(String pathToFile){
this.file = new File(pathToFile);
}
public File getFile(){
return file;
}
}
解决方案 3:
public class Foo{
private final String pathToFile;
private File file = null;
public class Foo(String pathToFile){
this.pathToFile = pathToFile;
}
public File getFile(){
if (file == null){
file = new File(pathToFile);
}
return file;
}
}
I was wondering if someone could explain the best solution for the smallest memory footprint for an object that has a file in the following situation...
- There could be 1 to a few hundred Foo classes.
- Thread safety will be important down the road.
- Not every Foo class's file is accessed every time.
- Each file is unique.
- The file in a Foo class may be accessed more than once.
I was planning to profile the solutions below to find the lowest memory footprint and i have a good idea which one would work best but I was interested in some feedback. Solution 1 seems like the best approach but it feels prone to memory leaks the more something accesses the getter. Thoughts?
Solution 1:
public class Foo{
private final String pathToFile;
public class Foo(String pathToFile){
this.pathToFile = pathToFile;
}
public File getFile(){
return new File(pathToFile);
}
}
Solution 2:
public class Foo{
private final File file;
public class Foo(String pathToFile){
this.file = new File(pathToFile);
}
public File getFile(){
return file;
}
}
Solution 3:
public class Foo{
private final String pathToFile;
private File file = null;
public class Foo(String pathToFile){
this.pathToFile = pathToFile;
}
public File getFile(){
if (file == null){
file = new File(pathToFile);
}
return file;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这完全取决于您想要对程序做什么,如果您需要其他地方的路径,那么您应该有一个参考。如果您需要该文件,您将再次需要参考。您可以做的另一个解决方案是在第二个解决方案中有一个将返回路径的方法:
file.getPath();
所以总的来说,第一个解决方案(如果您在某个时候需要路径),
或解决方案 2(如果不这样做)。
It all depens on what you want to do with the program, If you need the path in other places then you should have a reference to that. if you need the file, again you would need a reference. Another solution you could do is in the second solution have a method that will return the path:
file.getPath();
So overall either the first solution (if you need the path at some point),
or solution 2 if you do not.
不管怎样,这确实不应该有什么大不了的。第一个选项将在每次调用时创建一个新的文件引用,因此如果您调用此选项 100,000 次并保留对文件的引用,那么它可能会产生影响。否则,它仅取决于对 Foo 对象文件的引用是否有意义,或者 Foo 是否更像是一种服务而不是对象,并且其目标是返回对文件的任何合理引用。
It really shouldn't make a big deal either way. The first option will create a new file reference every time that its called, so if you call this 100,000 times AND keep the reference to the files, then it might make an impact. Otherwise, it just depends on if it makes sense to have a reference to the Foo objects file, or if Foo is more of a service than an object and its goal is to return any reasonable reference to the file.