Re: memory problems
jj wrote:
Hi
I have a very simple function that creates memory, do something with
it, and returns:
static void test(int k)
{
byte [] buff = new byte[k];
//do some stuff with buff[]
buff=null;
}
After a few calls to this function, it runs out memory
In C++ I would use delete at the end, and here in java I've been told
that GC takes care of it, but it seems that it does not
Am I doing something wrong? how can I free this temp memory after I
have used it?
The most likely explanation is that something in "do some stuff", or
something it calls, keeps a reference to each byte[].
I would debug this by trying to write a minimal program reproducing the
problem. Strip out as much as you can of "do some stuff" without making
the problem go away. Anything whose removal prevents the problem should
be investigated further.
The following program terminates successfully after allocating a total
of 1e10 bytes, in a 32 bit pointer environment. Obviously, it has to be
recycling the buffers.
public class MemoryTest {
public static void main(String[] args) {
int bufferSize = 10000000;
int iterations = 1000;
long start = System.nanoTime();
for (int i = 0; i < iterations; i++) {
test(bufferSize);
}
long end = System.nanoTime();
System.out.printf(
"Terminated after allocating %g bytes"
+ " in %g seconds%n", (double) bufferSize
* iterations, (end - start) / 1e9);
}
static void test(int k) {
byte[] buff = new byte[k];
buff[buff.length - 1] += buff[1];
}
}