Static methods and GC
Hello,
I've run into memory leak problem in my Java app. Before I start memory
profiling to know for sure I just wanted to ask You whether it is
reasonable to think that static methods can be causing this problem.
App design is as follows:
public static void main() {
...
for(...)
theStaticFunction();
...
}
private static void theStaticFunction() {
...
T[] memoryConsumingArray = ....
MyThread t = new MyThread(memoryConsumingArray);
t.start();
...
}
I'll provide you little explanation: In Main function some function
which is private and static is executed in loop, possibly many times.
Function creates local array which consumes a lot of heap memory (not
only because it has many elements but also every element contains large
object graph). Next this array is paased to thread constructor and
thread does further processing. Array or its elements are not assigned
to any static fields, they exist only in theStaticFunction, so should
be not reachable from roots of gc as soon as all threads finish they
work. Yet, program crashes with OutOfMemory from time to time unless it
is given 1GB of RAM or so, which is little too much :-) So I am trying
to examine potential memory leaks. The first suspect is this array, as
it is dominating factor of memory consumption. Do you think that
problem might lie in large array being used from static function?