1 /* |
1 /* |
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
|
24 import java.io.*; |
|
25 |
24 public class RedefineBigClassApp { |
26 public class RedefineBigClassApp { |
|
27 /** |
|
28 * Memory leak is assumed, if application consumes more than specified amount of memory during its execution. |
|
29 * The number is given in Kb. |
|
30 */ |
|
31 private static final long MEM_LEAK_THRESHOLD = 32 * 1024; // 32Mb |
|
32 |
25 public static void main(String[] args) throws Exception { |
33 public static void main(String[] args) throws Exception { |
26 System.out.println("Creating instance of " + |
34 System.out.println("Creating instance of " + |
27 RedefineBigClassAgent.clz); |
35 RedefineBigClassAgent.clz); |
28 RedefineBigClassAgent.clz.newInstance(); |
36 RedefineBigClassAgent.clz.newInstance(); |
29 |
37 |
|
38 long vMemBefore = getVMemSize(); |
30 int count = 0; |
39 int count = 0; |
31 while (!RedefineBigClassAgent.doneRedefining) { |
40 while (!RedefineBigClassAgent.doneRedefining) { |
32 System.out.println("App loop count: " + ++count); |
41 System.out.println("App loop count: " + ++count); |
33 try { |
42 try { |
34 Thread.sleep(10 * 1000); |
43 Thread.sleep(10 * 1000); |
35 } catch (InterruptedException ie) { |
44 } catch (InterruptedException ie) { |
36 } |
45 } |
37 } |
46 } |
38 System.out.println("App looped " + count + " times."); |
47 System.out.println("App looped " + count + " times."); |
39 |
48 |
|
49 long vMemAfter = getVMemSize(); |
|
50 if (vMemBefore == 0 || vMemAfter == 0) { |
|
51 System.err.println("WARNING: Cannot perform memory leak detection on this OS"); |
|
52 } else { |
|
53 long vMemDelta = vMemAfter - vMemBefore; |
|
54 if (vMemDelta > MEM_LEAK_THRESHOLD) { |
|
55 System.err.println("FAIL: Virtual memory usage increased by " + vMemDelta + "Kb " + |
|
56 "(greater than " + MEM_LEAK_THRESHOLD + "Kb)"); |
|
57 System.exit(1); |
|
58 } |
|
59 System.err.println("PASS: Virtual memory usage increased by " + vMemDelta + "Kb " + |
|
60 "(not greater than " + MEM_LEAK_THRESHOLD + "Kb)"); |
|
61 } |
40 System.exit(0); |
62 System.exit(0); |
41 } |
63 } |
|
64 |
|
65 /** |
|
66 * Return size of virtual memory allocated to the process in Kb. |
|
67 * Linux specific. On other platforms and in case of any errors return 0. |
|
68 */ |
|
69 private static long getVMemSize() { |
|
70 |
|
71 // Refer to the Linux proc(5) man page for details about /proc/self/stat file |
|
72 // |
|
73 // In short, this file contains status information about the current process |
|
74 // written in one line. The fields are separated with spaces. |
|
75 // The 23rd field is defined as 'vsize %lu Virtual memory size in bytes' |
|
76 |
|
77 try (FileReader fileReader = new FileReader("/proc/self/stat"); |
|
78 BufferedReader bufferedReader = new BufferedReader(fileReader)) { |
|
79 String line = bufferedReader.readLine(); |
|
80 return Long.parseLong(line.split(" ")[22]) / 1024; |
|
81 } catch (Exception ex) {} |
|
82 return 0; |
|
83 } |
42 } |
84 } |