Skip to content

Instantly share code, notes, and snippets.

@tamtam180
Created June 23, 2012 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamtam180/2976972 to your computer and use it in GitHub Desktop.
Save tamtam180/2976972 to your computer and use it in GitHub Desktop.
リークの確認テスト
public class Leak {
public static void main(String[] args) throws Exception {
File tmpDir = new File("mytmp");
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}
// 無理やりPIDを取得。Linuxのみ。
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
int pid = Integer.parseInt(bean.getName().split("@")[0]);
for (int i = 0; i < 100; i++) {
String sessionId = makeSessionId();
File file = File.createTempFile(sessionId, ".pipeout", tmpDir);
BufferedReader br = new BufferedReader(new FileReader(file));
// OPENしっぱなし
if (i % 10 == 0) {
System.gc();
System.out.println("exec GC.");
}
System.out.printf("%d, %d%n", i, getLsof(pid).size());
TimeUnit.MILLISECONDS.sleep(100);
}
}
// Hiveの処理をそのまま
private static String makeSessionId() {
GregorianCalendar gc = new GregorianCalendar();
String userid = "tamtam";
return userid
+ "_"
+ String.format("%1$4d%2$02d%3$02d%4$02d%5$02d",
gc.get(Calendar.YEAR), gc.get(Calendar.MONTH) + 1,
gc.get(Calendar.DAY_OF_MONTH),
gc.get(Calendar.HOUR_OF_DAY), gc.get(Calendar.MINUTE));
}
// ここ手抜き処理なので、終了処理とかいい加減です。
// 真似しちゃだめ。
public static List<String> getLsof(int pid) throws Exception {
LinkedList<String> list = new LinkedList<String>();
ProcessBuilder pb = new ProcessBuilder();
pb.command("lsof", "-p", String.valueOf(pid));
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
p.waitFor();
p.destroy();
return list;
}
}
exec GC.
0, 34
1, 35
2, 36
3, 37
4, 38
5, 39
6, 40
7, 41
8, 42
9, 43
exec GC.
10, 34
11, 35
12, 36
13, 37
14, 38
15, 39
16, 40
17, 41
18, 42
19, 43
exec GC.
20, 34
21, 35
22, 36
23, 37
24, 38
25, 39
26, 40
27, 41
28, 42
29, 43
exec GC.
30, 34
31, 35
32, 36
33, 37
34, 38
35, 39
36, 40
37, 41
38, 42
39, 43
exec GC.
40, 34
41, 35
42, 36
43, 37
44, 38
45, 39
46, 40
47, 41
48, 42
49, 43
exec GC.
50, 34
51, 35
52, 36
53, 37
54, 38
55, 39
56, 40
57, 41
58, 42
59, 43
exec GC.
60, 34
61, 35
62, 36
63, 37
64, 38
65, 39
66, 40
67, 41
68, 42
69, 43
exec GC.
70, 34
71, 35
72, 36
73, 37
74, 38
75, 39
76, 40
77, 41
78, 42
79, 43
exec GC.
80, 34
81, 35
82, 36
83, 37
84, 38
85, 39
86, 40
87, 41
88, 42
89, 43
exec GC.
90, 34
91, 35
92, 36
93, 37
94, 38
95, 39
96, 40
97, 41
98, 42
99, 43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment