Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/GarbageGenerator.java
41155 views
/*1* Copyright (c) 2018, 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*/2223package nsk.stress.jni;2425class GarbageGenerator extends Thread {26class Garbage {27Garbage() {28this(1024);29}3031Garbage(int m) {32memory = new byte[m];33}3435void setNext(Garbage n) {36next = n;37}3839Garbage getNext() {40return next;41}4243protected void finalize() {44}4546private Garbage next;47private byte[] memory;48}4950class GarbageRing {51GarbageRing() {52attachment = new Garbage(0);53}5455void add(int size) {56Garbage head = attachment.getNext();57Garbage g = new Garbage(size);58if (head != null) {59Garbage oldNext = head.getNext();60if (oldNext != null) {61g.setNext(oldNext);62head.setNext(g);63attachment.setNext(g);64} else {65g.setNext(head);66head.setNext(g);67}68} else69attachment.setNext(g);70}7172void discard() {73attachment.setNext(null);74}7576private byte[] memory;77private Garbage attachment;78}7980public void run() {81GarbageRing gr = new GarbageRing();82int g = 0;83while (!done) {84for (g = 0; g < ringSize; g++) {85gr.add(allocSize);86Thread.yield();87}88gr.discard();89try {90sleep(interval);91} catch (InterruptedException e) {92}93}94if (DEBUG) System.out.println("GarbageRing::run(): done");95}9697public void setAllocSize(int i) {98allocSize = i;99}100101public int getAllocSize() {102return allocSize;103}104105public void setInterval(int i) {106interval = i;107}108109public int getInterval() {110return interval;111}112113public void halt() {114done = true;115}116117private int allocSize = 10000;118private int ringSize = 50;119private int interval = 1000;120private boolean done = false;121final private static boolean DEBUG = false;122}123124125