Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstat/utils/GcProvoker.java
41159 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package utils;2324import java.util.ArrayList;25import java.util.List;2627/**28* This is an class used to provoke GC and perform other GC-related29* procedures30*31*/32public class GcProvoker{3334// Uses fixed small objects to avoid Humongous objects allocation in G135private static final int MEMORY_CHUNK = 2048;3637private final Runtime runtime;3839private List<Object> allocateHeap(float targetUsage) {40long maxMemory = runtime.maxMemory();41List<Object> list = new ArrayList<>();42long used = 0;43long target = (long) (maxMemory * targetUsage);44while (used < target) {45try {46list.add(new byte[MEMORY_CHUNK]);47used += MEMORY_CHUNK;48} catch (OutOfMemoryError e) {49list = null;50throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "' while eating " + targetUsage + " of heap memory.");51}52}53return list;54}5556/**57* This method provokes a GC58*/59public void provokeGc() {60for (int i = 0; i < 3; i++) {61long edenSize = Pools.getEdenCommittedSize();62long heapSize = Pools.getHeapCommittedSize();63float targetPercent = ((float) edenSize) / (heapSize);64if ((targetPercent < 0) || (targetPercent > 1.0)) {65throw new RuntimeException("Error in the percent calculation" + " (eden size: " + edenSize + ", heap size: " + heapSize + ", calculated eden percent: " + targetPercent + ")");66}67allocateHeap(targetPercent);68allocateHeap(targetPercent);69System.gc();70}71}7273public GcProvoker() {74runtime = Runtime.getRuntime();75}76}777879