|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Relates :
|
|
|
Relates :
|
|
|
Relates :
|
|
|
Relates :
|
|
|
Relates :
|
Continually looping on an unavailable semaphore,
for example, with a timed tryAcquire, continues to consume memory.
------------------------------------
public class Leak6 {
public static void main(String [] args) throws Throwable {
final int n = 1000*1000;
long mem = Runtime.getRuntime().freeMemory();
System.out.println("Free: " + mem);
Semaphore s = new Semaphore(0);
int i = 0;
try {
while (++i < n)
s.tryAcquire(1,TimeUnit.MICROSECONDS);
} catch (OutOfMemoryError oome) {
System.out.printf("OOME on iteration %d%n", i);
}
System.gc();
long mem2 = Runtime.getRuntime().freeMemory();
System.out.println("Memory used: " + (mem2 - mem));
}
}
------------------------------------
==> java -Xmx16m -esa -ea Leak6
Free: 7675240
OOME on iteration 488232
Memory used: 5888064
|