Java Code Examples for sun.tools.attach.HotSpotVirtualMachine#heapHisto()

The following examples show how to use sun.tools.attach.HotSpotVirtualMachine#heapHisto() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuntimeMXBeanTest.java    From code with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, AttachNotSupportedException {

        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String name = bean.getName();
        int index = name.indexOf('@');
        String pid = name.substring(0, index);
        //这里要区分操作系统
        HotSpotVirtualMachine machine = (HotSpotVirtualMachine) new sun.tools.attach.WindowsAttachProvider().attachVirtualMachine(pid);
        InputStream is = machine.heapHisto("-all");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int len;
        byte[] buff = new byte[1024];
        while ((len = is.read(buff)) > 0)
            os.write(buff, 0, len);
        is.close();
        machine.detach();
        System.out.println(os);

    }
 
Example 2
Source File: TestLoggerWeakRefLeak.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 3
Source File: TestLoggerWeakRefLeak.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 4
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 5
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 6
Source File: TestLoggerWeakRefLeak.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Long.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}
 
Example 7
Source File: TestLoggerWeakRefLeak.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 'vm.heapHisto("-live")' will request a full GC
 */
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
    int instanceCount = 0;

    HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
            .attach(Integer.toString(ProcessTools.getProcessId()));
    try {
        try (InputStream heapHistoStream = vm.heapHisto("-live");
                BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                if (inputLine.contains(TARGET_CLASS)) {
                    instanceCount = Integer.parseInt(inputLine
                            .split("[ ]+")[2]);
                    System.out.println("instance count: " + instanceCount);
                    break;
                }
            }
        }
    } finally {
        vm.detach();
    }

    assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");

    return instanceCount;
}