java静态成员内存泄漏
我正在审查一个java代码库以查找一些内存泄漏。在审查过程中我发现了以下场景。
- 代码库中有一个类 Class1。 Class1 有一些实例变量和一些静态成员。一些其他类创建 Class1 的实例。这种情况会导致内存泄漏吗?
- 代码库中提供了 Class2 类。它有一些公共静态内部类。静态内部类的实例是从其他静态内部类创建的。会导致内存泄漏吗?例如,
Class2 { 公共静态类Class3 { } 公共静态类Class4 { } 公共静态类Class3 { Class3 c = new Class3(); //.... } 公共静态 int doSomething1{ } 公共静态无效 doSomething2{ } 公共无效doSomething3 { } }
有人可以给出答案吗?
I am reviewing one java code base for finding some memory leaks. During the review I have find the following scenarios.
- A class Class1 has in the codebase. Class1 have some instance variables and some static members. Some other classes creating the instances of the Class1. Is this scenario leads to any memory leak?
- A class Class2 available in the codebase. It have some public static inner classes. And the instance of the static inner classes is creating from the other static inner classes. Does it lead to memory leak? For example,
Class2 { public static class Class3 { } public static class Class4 { } public static class Class3 { Class3 c = new Class3(); //…. } public static int doSomething1{ } public static void doSomething2{ } public void doSomething3{ } }
Can somebody give answers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建其他类的实例本身不会造成内存泄漏。
保留引用的时间超过需要的时间会导致内存泄漏。
这些引用可以是显式的或隐式的。例如:如果您创建非静态内部类的实例,那么它将保留对外部实例的引用,即使不存在对该外部实例的显式引用。
因此,您的直接问题的答案是明确的:也许。您需要向我们提供更多信息。
并且:查找内存泄漏的一个非常好的工具是使用探查器。特别是如果这是一个大内存泄漏。
Creating instances of other classes doesn't in itself create memory leaks.
Holding on to references longer than needed creates memory leaks.
Those references can be explicit or implicit. For example: if you create an instance of a non-static inner class then it will keep a reference to the outer instance, even if no explicit reference to that exists.
So the answer to your direct question is a definite: maybe. You need to give us more information.
And: a pretty good tool to find memory leaks is using a profiler. Especially if it's a big memory leak.
一般来说,静态成员只有设置为null后才会被释放。如果实例本身不可访问,则实例变量将被释放。
静态内部类就像普通的类一样,因此遵守上述规则。
非静态内部类如@Joachim Sauer 所描述。
ps 学习使用分析器,它将让你在余下的编程生涯中受益:)
Generally speaking, static members will not be released until you set them to null. Instance variables will be released if the instance itself is unreachable.
Static inner classes are just like a normal class and therefore obeys the rule above.
Non-static inner class is as describe by @Joachim Sauer.
p.s. Learn to use a profiler, it'll benefit you for the rest of your programming life :)
我遇到了内存泄漏的问题。一位同事推荐了一个内存分析工具http://www.eclipse.org/mat/。
我不是java大师,甚至还算不上。但我所做的是运行我的代码,监视程序何时崩溃(内存不足),然后我将再次运行代码,但这一次,使用命令行选项
-Xmx
到一个数字我知道会使程序崩溃。我还添加了
-XX:+HeapDumpOnOutOfMemoryError
,然后当程序崩溃时,使用分析工具并对堆转储进行分析。我能够找到有问题的变量/对象。希望有帮助,祝你好运!
I had an issue with memory leak. a colleague recommended a memory profiling tool http://www.eclipse.org/mat/.
I'm no java master, not even close. But what I did was ran my code, monitored for when the program will crash (out of memory), then I would run the code again, but this time, using command line option
-Xmx
to a number I know would crash the program.I added too the
-XX:+HeapDumpOnOutOfMemoryError
, then when the program crash, used the profiling tool and profiled the heap dump.I was able to find the offending variables/objects. hope that helps, good luck!