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 } | 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.util.ArrayList; 25 import java.util.List; 26 import jdk.test.lib.process.ProcessTools; 27 28 import sun.management.VMManagement; 29 30 public class JMapHProfLargeHeapProc { 31 private static final List<byte[]> heapGarbage = new ArrayList<>(); 32 33 public static void main(String[] args) throws Exception { 34 35 buildLargeHeap(args); 36 37 // Print our pid on stdout 38 System.out.println("PID[" + ProcessTools.getProcessId() + "]"); 39 40 // Wait for input before termination 41 System.in.read(); 42 } 43 44 private static void buildLargeHeap(String[] args) { 45 for (long i = 0; i < Integer.parseInt(args[0]); i++) { 46 heapGarbage.add(new byte[1024]); 47 } 48 } 49 50 } |