|
1 /* |
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
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 |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import java.lang.management.ManagementFactory; |
|
25 import java.lang.management.RuntimeMXBean; |
|
26 import java.lang.reflect.Field; |
|
27 import java.lang.reflect.Method; |
|
28 import java.util.ArrayList; |
|
29 import java.util.List; |
|
30 |
|
31 import sun.management.VMManagement; |
|
32 |
|
33 public class JMapHProfLargeHeapProc { |
|
34 private static final List<byte[]> heapGarbage = new ArrayList<>(); |
|
35 |
|
36 public static void main(String[] args) throws Exception { |
|
37 |
|
38 buildLargeHeap(args); |
|
39 |
|
40 // Print our pid on stdout |
|
41 System.out.println("PID[" + getProcessId() + "]"); |
|
42 |
|
43 // Wait for input before termination |
|
44 System.in.read(); |
|
45 } |
|
46 |
|
47 private static void buildLargeHeap(String[] args) { |
|
48 for (long i = 0; i < Integer.parseInt(args[0]); i++) { |
|
49 heapGarbage.add(new byte[1024]); |
|
50 } |
|
51 } |
|
52 |
|
53 public static int getProcessId() throws Exception { |
|
54 |
|
55 // Get the current process id using a reflection hack |
|
56 RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); |
|
57 Field jvm = runtime.getClass().getDeclaredField("jvm"); |
|
58 |
|
59 jvm.setAccessible(true); |
|
60 VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime); |
|
61 |
|
62 Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId"); |
|
63 |
|
64 pid_method.setAccessible(true); |
|
65 |
|
66 int pid = (Integer) pid_method.invoke(mgmt); |
|
67 |
|
68 return pid; |
|
69 } |
|
70 |
|
71 } |