blob: 9b3b2f82859709f76ef72a58c402e10937554ad2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class NestedAroundClosureMemoryLeakTest {
private static final int NUM_THREAD_POOLS = 4;
private static final int THREAD_POOL_SIZE = 3;
public static void main(String[] args) throws Exception {
testNoMemoryLeak_ThreadLocalCleared();
}
/**
* Tests that the thread-locals of the spawned threads are either null or contain all null elements
*/
public static void testNoMemoryLeak_ThreadLocalCleared() throws Exception {
List<ExecutorService> executorServices = createExecutorServicesWithFixedThreadPools();
try {
executeTasks(executorServices);
Field mapField = Thread.class.getDeclaredField("threadLocals");
mapField.setAccessible(true);
Set<Thread> threads = Thread.getAllStackTraces().keySet();
System.out.println("Number of pool threads = " + threads.stream().filter(thread -> thread.getName().contains("pool")).count());
threads.stream()
.filter(thread -> thread.getName().contains("pool"))
.forEach(thread -> {
try {
Object threadLocals = mapField.get(thread);
if (threadLocals != null) {
Field tableField = threadLocals.getClass().getDeclaredField("table");
tableField.setAccessible(true);
Object[] threadLocalTable = (Object[]) tableField.get(threadLocals);
if (threadLocalTable != null) {
for (Object entry : threadLocalTable) {
if (entry == null)
continue;
Field entryValueField = entry.getClass().getDeclaredField("value");
entryValueField.setAccessible(true);
throw new RuntimeException(
"All thread-locals should be null, but found entry with value " + entryValueField.get(entry)
);
}
}
}
}
catch (RuntimeException rte) {
throw rte;
}
catch (Exception e) {
throw new RuntimeException(e);
}
});
System.out.println("Test passed - all thread-locals are null");
}
finally {
for (ExecutorService executorService : executorServices)
executorService.shutdown();
}
}
private static List<ExecutorService> createExecutorServicesWithFixedThreadPools() {
List<ExecutorService> executorServiceList = new ArrayList<>(NestedAroundClosureMemoryLeakTest.NUM_THREAD_POOLS);
for (int i = 0; i < NestedAroundClosureMemoryLeakTest.NUM_THREAD_POOLS; i++)
executorServiceList.add(Executors.newFixedThreadPool(THREAD_POOL_SIZE));
return executorServiceList;
}
private static void executeTasks(List<ExecutorService> executorServices) throws Exception {
for (ExecutorService executorService : executorServices) {
for (int i = 0; i < THREAD_POOL_SIZE * 2; i++)
new Task(executorService).doSomething();
}
System.out.println("Finished executing tasks");
// Sleep to take a memory dump
// Thread.sleep(500000);
}
}
|