-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamplingExample.java
77 lines (65 loc) · 2.02 KB
/
SamplingExample.java
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
import java.util.Stack;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.nio.file.*;
class SamplingExample {
private static Stack<A> stk = new Stack<>();
private static AtomicInteger c = new AtomicInteger();
// 16 + 14*8 = 128
static class A {
private double d1;
private double d2;
private double d3;
private double d4;
private double d5;
private double d6;
private double d7;
private double d8;
private double d9;
private double d10;
private double d11;
private double d12;
private double d13;
private double d14;
}
private static A culprit() {
A a = new A();
if (SamplingExample.c.getAndIncrement() % 2 == 0) {
SamplingExample.stk.push(a);
}
return a;
}
private static A warmup() {
var n = 8*1024*10;
for (int i = 0; i < n; i++) {
SamplingExample.stk.push(new A());
}
for (int i = 0; i < n - 1; i++) {
SamplingExample.stk.pop();
}
return SamplingExample.stk.pop();
}
public static void main(String[] args) throws Exception {
var a = new AtomicReference<A>();
var exec = Executors.newFixedThreadPool(2);
exec.submit(() -> {}).get();
// Start sampling in the background
Future<byte[]> profileResult = Heapz.sampleFor(5);
a.set(SamplingExample.warmup());
Runnable task = () -> {
for (int i = 0; i < 8*1024*12; i++) {
a.set(SamplingExample.culprit());
}
};
exec.execute(task);
// exec.execute(task);
// Wait until sample is collected
byte[] profile = profileResult.get();
Path path = Paths.get("sample.prof");
Files.write(path, profile);
exec.shutdown();
Heapz.shutdown();
}
}