Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.caliper.bridge; |
| 18 | |
| 19 | import com.sun.management.HotSpotDiagnosticMXBean; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.lang.management.ManagementFactory; |
| 23 | |
| 24 | import javax.management.MBeanServer; |
| 25 | |
| 26 | /** |
| 27 | * A simple class that invokes {@link System#gc()} over and over to generate some GC log messages. |
| 28 | */ |
| 29 | public final class GcLogMessageGenerator { |
| 30 | public static void main(String[] args) throws IOException { |
| 31 | checkGcLogging(); |
| 32 | for (int i = 0; i < 100; i++) { |
| 33 | System.gc(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; |
| 38 | |
| 39 | private static void checkGcLogging() throws IOException { |
| 40 | MBeanServer server = ManagementFactory.getPlatformMBeanServer(); |
| 41 | HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy( |
| 42 | server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); |
| 43 | if (!bean.getVMOption("PrintGC").getValue().equals(Boolean.TRUE.toString())) { |
| 44 | System.err.println("This is only useful if you run with -XX:+PrintGC"); |
| 45 | System.exit(1); |
| 46 | } |
| 47 | } |
| 48 | } |