--- old/src/share/vm/services/lowMemoryDetector.cpp Wed Jun 7 14:51:58 2017 +++ new/src/share/vm/services/lowMemoryDetector.cpp Wed Jun 7 14:51:58 2017 @@ -296,19 +296,41 @@ Klass* k = Management::sun_management_Sensor_klass(CHECK); instanceKlassHandle sensorKlass (THREAD, k); Handle sensor_h(THREAD, _sensor_obj); - Handle usage_h = MemoryService::create_MemoryUsage_obj(_usage, CHECK); + Symbol* trigger_method_signature; + JavaValue result(T_VOID); JavaCallArguments args(sensor_h); args.push_int((int) count); - args.push_oop(usage_h); + Handle usage_h = MemoryService::create_MemoryUsage_obj(_usage, THREAD); + // call Sensor::trigger(int, MemoryUsage) to send notification to listeners. + // when OOME occurs and fails to allocate MemoryUsage object, call + // Sensor::trigger(int) instead. The pending request will be processed + // but no notification will be sent. + if (HAS_PENDING_EXCEPTION) { + assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOME here"); + CLEAR_PENDING_EXCEPTION; + trigger_method_signature = vmSymbols::int_void_signature(); + } else { + trigger_method_signature = vmSymbols::trigger_method_signature(); + args.push_oop(usage_h); + } + JavaCalls::call_virtual(&result, - sensorKlass, - vmSymbols::trigger_name(), - vmSymbols::trigger_method_signature(), - &args, - CHECK); + sensorKlass, + vmSymbols::trigger_name(), + trigger_method_signature, + &args, + THREAD); + + if (HAS_PENDING_EXCEPTION) { + // we just clear the OOM pending exception that we might have encountered + // in Java's tiggerAction(), and continue with updating the counters since + // the Java counters have been updated too. + assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOME here"); + CLEAR_PENDING_EXCEPTION; + } } { --- /dev/null Wed Jun 7 14:51:59 2017 +++ new/test/runtime/ServiceThread/ServiceThread.java Wed Jun 7 14:51:59 2017 @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +import java.lang.management.*; +import com.sun.management.OperatingSystemMXBean; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; + +public class ServiceThread { + public static void main(String[] args) throws InterruptedException { + + ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(new NotificationListener() { + + @Override + public void handleNotification(Notification pNotification, Object pHandback) { + System.out.println("notification..."); + } + }, null, null); + + for (MemoryPoolMXBean m : ManagementFactory.getMemoryPoolMXBeans()) { + if (m.getType() == MemoryType.HEAP) { + MemoryUsage localMemoryUsage = m.getUsage(); + long l = (long) (localMemoryUsage.getMax() == -1L ? + Runtime.getRuntime().maxMemory() * 0.9D : localMemoryUsage.getMax() * 0.9D); + m.setCollectionUsageThreshold(l); + } + } + + ExecutorService pool = Executors.newFixedThreadPool(5); + List> futures = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + futures.add(pool.submit(new Runnable() { + @Override + public void run() { + try { + Random random = new Random(); + List interned = new ArrayList<>(); + while (true) { + int length = random.nextInt(100); + StringBuilder builder = new StringBuilder(); + String sample = "whoisyourdaddy"; + for (int i = 0; i < length; i++) { + builder.append(sample.charAt(random.nextInt(sample.length()))); + } + interned.add(builder.toString().intern()); + } + + } catch (Throwable t) { + System.out.println(Thread.currentThread().getName() + ": " + t); + throw t; + } + } + })); + } + + int m = 0; + while (m < 2) { + Thread.sleep(60000); + m++; + final Iterator> iter = futures.iterator(); + while (iter.hasNext()) { + Future future = iter.next(); + System.out.println("\tfuture task DONE: " + future.isDone()); + if (future.isDone()) { + iter.remove(); + } + } + if (futures.isEmpty()) { + if (!pool.isShutdown()) { + System.out.println("\tshutting down thread pool..."); + pool.shutdown(); + } + } + } + + OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean(); + double load = os.getProcessCpuLoad()*100; + System.out.println("Process CPU Load: "+ load); + if (load > 5){ + System.out.println("Test Failed..."); + System.exit(1); + } else { + System.out.println("Test Passed..."); + System.exit(0); + } + } +} + --- /dev/null Wed Jun 7 14:52:00 2017 +++ new/test/runtime/ServiceThread/ServiceThreadTest.java Wed Jun 7 14:52:00 2017 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8178536 + * @summary Service Thread should not consume CPU cycles in the event of an OOME + * @library /test/lib + * @modules java.base/jdk.internal.misc + * java.management + * @run main/timeout=350 ServiceThreadTest + */ + +import jdk.test.lib.process.ProcessTools; +import jdk.test.lib.process.OutputAnalyzer; + +public class ServiceThreadTest { + public static void main(String[] args) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xmx4m", "ServiceThread"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + System.out.println(output.getOutput()); + output.shouldContain("Test Passed"); + output.shouldHaveExitValue(0); + } +} + +