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
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?
To force garbage collection you can use System.gc()
This is not usually necessary - are you sure you aren't just trying to allocate an array that's too big anyway?
Of course, if one of the things you are doing in "some stuff" makes the array reachable, the garbage collector won't throw it away.
Also if I am correct, I think that the GC will only release the memory if less than 40% of the heap is used...
I don't think so, that would completely defeat the point of it.
Sorry... Trying to remember back to college is sometimes hard... I new that 40 was some number that had to do with garbage collection, so I did some quick research... And 40% and 70% have to do with resizing the heap when the JVM preforms gc();
Tuning Garbage Collection with the 1.4.2 Java[tm] Virtual Machine