Index: modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java =================================================================== --- modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java (revision 537921) +++ modules/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java (working copy) @@ -122,8 +122,15 @@ * @throws OutOfMemoryError * if the request cannot be satisfied. */ - public native long malloc(long length) throws OutOfMemoryError; + public long malloc(long length) throws OutOfMemoryError { + if (OSResourcesMonitor.isSystemPhysicalMemoryLowDefault()) { + System.gc(); + } + return mallocNative(length); + } + private native long mallocNative(long length) throws OutOfMemoryError; + /** * Deallocates space for a memory block that was previously allocated by a * call to {@link #malloc(long) malloc(long)}. The number of bytes freed is Index: modules/luni/src/main/java/org/apache/harmony/luni/platform/OSResourcesMonitor.java =================================================================== --- modules/luni/src/main/java/org/apache/harmony/luni/platform/OSResourcesMonitor.java (revision 0) +++ modules/luni/src/main/java/org/apache/harmony/luni/platform/OSResourcesMonitor.java (revision 0) @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.luni.platform; + +public class OSResourcesMonitor { + /** + * Returns true if the remaining physical free memory is less + * than the default thresholds and false otherwise. + * + * @return true if the remaining physical free memory is less + * than the default thresholds and false otherwise. + */ + public native static boolean isSystemPhysicalMemoryLowDefault(); + + /** + * Returns true if the remaining physical free memory is less + * than the given thresholds and false otherwise. + * + * @param loadThreshold Used memory threshold (in %) + * @param freeThreshold Available memory threshold + * + * @return true if the remaining physical free memory is less + * than the given thresholds and false otherwise. + */ + public native static boolean isSystemPhysicalMemoryLow(int loadThreshold, int freeThreshold); +} Index: modules/luni/src/main/native/luni/shared/OSResourcesMonitor.c =================================================================== --- modules/luni/src/main/native/luni/shared/OSResourcesMonitor.c (revision 0) +++ modules/luni/src/main/native/luni/shared/OSResourcesMonitor.c (revision 0) @@ -0,0 +1,46 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Utilities to monitor system resources. + */ + +#include "vmi.h" +#include "OSResourcesMonitor.h" + +/* + * Class: org_apache_harmony_luni_platform_OSResourcesMonitor + * Method: isSystemPhysicalMemoryLow + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_apache_harmony_luni_platform_OSResourcesMonitor_isSystemPhysicalMemoryLowDefault + (JNIEnv *env, jclass thizClass) +{ + PORT_ACCESS_FROM_ENV (env); + return hysysinfo_is_system_physical_memory_low_default() ? TRUE : FALSE; +} + +/* + * Class: org_apache_harmony_luni_platform_OSResourcesMonitor + * Method: isSystemPhysicalMemoryLow + * Signature: (II)Z + */ +JNIEXPORT jboolean JNICALL Java_org_apache_harmony_luni_platform_OSResourcesMonitor_isSystemPhysicalMemoryLow + (JNIEnv *env, jclass thizClass, jint load_threshold, jint free_threshold) +{ + PORT_ACCESS_FROM_ENV (env); + return hysysinfo_is_system_physical_memory_low(load_threshold, free_threshold) ? TRUE : FALSE; +} Index: modules/luni/src/main/native/luni/shared/OSResourcesMonitor.h =================================================================== --- modules/luni/src/main/native/luni/shared/OSResourcesMonitor.h (revision 0) +++ modules/luni/src/main/native/luni/shared/OSResourcesMonitor.h (revision 0) @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +/* Header for class org_apache_harmony_luni_platform_OSResourcesMonitor */ + +#ifndef _Included_org_apache_harmony_luni_platform_OSResourcesMonitor +#define _Included_org_apache_harmony_luni_platform_OSResourcesMonitor +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_apache_harmony_luni_platform_OSResourcesMonitor + * Method: isSystemPhysicalMemoryLow + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_apache_harmony_luni_platform_OSResourcesMonitor_isSystemPhysicalMemoryLowDefault + (JNIEnv *, jclass); + +/* + * Class: org_apache_harmony_luni_platform_OSResourcesMonitor + * Method: isSystemPhysicalMemoryLow + * Signature: (IJ)Z + */ +JNIEXPORT jboolean JNICALL Java_org_apache_harmony_luni_platform_OSResourcesMonitor_isSystemPhysicalMemoryLow + (JNIEnv *, jclass, jint, jint); + +#ifdef __cplusplus +} +#endif +#endif Index: modules/luni/src/main/native/luni/unix/exports.txt =================================================================== --- modules/luni/src/main/native/luni/unix/exports.txt (revision 537921) +++ modules/luni/src/main/native/luni/unix/exports.txt (working copy) @@ -179,7 +179,7 @@ Java_org_apache_harmony_luni_platform_OSMemory_getPointerSizeImpl Java_org_apache_harmony_luni_platform_OSMemory_getAddress Java_org_apache_harmony_luni_platform_OSMemory_setAddress -Java_org_apache_harmony_luni_platform_OSMemory_malloc +Java_org_apache_harmony_luni_platform_OSMemory_mallocNative Java_org_apache_harmony_luni_platform_OSMemory_free Java_org_apache_harmony_luni_platform_OSMemory_memmove Java_org_apache_harmony_luni_platform_OSMemory_memset @@ -247,5 +247,7 @@ Java_org_apache_harmony_luni_platform_OSNetworkSystem_isReachableByICMPImpl Java_org_apache_harmony_luni_platform_OSNetworkSystem_inheritedChannelImpl Java_org_apache_harmony_luni_platform_OSNetworkSystem_oneTimeInitializationImpl +Java_org_apache_harmony_luni_platform_OSResourcesMonitor_isSystemPhysicalMemoryLow +Java_org_apache_harmony_luni_platform_OSResourcesMonitor_isSystemPhysicalMemoryLowDefault Java_org_apache_harmony_luni_platform_Environment_getEnvBytes Java_org_apache_harmony_luni_platform_Environment_getEnvByName Index: modules/luni/src/main/native/luni/unix/makefile =================================================================== --- modules/luni/src/main/native/luni/unix/makefile (revision 537921) +++ modules/luni/src/main/native/luni/unix/makefile (working copy) @@ -34,7 +34,7 @@ $(SHAREDSUB)filedesc.o $(SHAREDSUB)timezone.o \ $(SHAREDSUB)OSFileSystem.o OSFileSystemLinux32.o \ OSMemory.o OSMemoryLinux32.o $(SHAREDSUB)OSNetworkSystem.o \ - OSNetworkSystemLinux.o hyenv.o + OSNetworkSystemLinux.o $(SHAREDSUB)OSResourcesMonitor.o hyenv.o MDLLIBFILES += \ $(LIBPATH)libhyzip.a $(DLLPATH)libhyzlib$(HY_LINKLIB_SUFFIX) \ Index: modules/luni/src/main/native/luni/unix/OSMemory.c =================================================================== --- modules/luni/src/main/native/luni/unix/OSMemory.c (revision 537921) +++ modules/luni/src/main/native/luni/unix/OSMemory.c (working copy) @@ -26,7 +26,7 @@ #include "IMemorySystem.h" #include "exceptions.h" -JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_malloc +JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mallocNative (JNIEnv * env, jobject thiz, jlong size) { PORT_ACCESS_FROM_ENV (env); Index: modules/luni/src/main/native/luni/unix/OSMemory.h =================================================================== --- modules/luni/src/main/native/luni/unix/OSMemory.h (revision 537921) +++ modules/luni/src/main/native/luni/unix/OSMemory.h (working copy) @@ -43,10 +43,10 @@ (JNIEnv *, jclass); /* * Class: org_apache_harmony_luni_platform_OSMemory - * Method: malloc + * Method: mallocNative * Signature: (J)J */ - JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_malloc + JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mallocNative (JNIEnv *, jobject, jlong); /* * Class: org_apache_harmony_luni_platform_OSMemory Index: modules/luni/src/main/native/luni/windows/makefile =================================================================== --- modules/luni/src/main/native/luni/windows/makefile (revision 537921) +++ modules/luni/src/main/native/luni/windows/makefile (working copy) @@ -37,7 +37,7 @@ $(SHAREDSUB)filedesc.obj $(SHAREDSUB)timezone.obj \ OSFileSystemWin32.obj hyenv.obj\ $(SHAREDSUB)OSFileSystem.obj OSMemoryWin32.obj OSMemory.obj \ - $(SHAREDSUB)OSNetworkSystem.obj OSNetworkSystemWin32.obj + $(SHAREDSUB)OSNetworkSystem.obj OSNetworkSystemWin32.obj $(SHAREDSUB)OSResourcesMonitor.obj VIRTFILES = hyluni.res Index: modules/luni/src/main/native/luni/windows/OSMemory.c =================================================================== --- modules/luni/src/main/native/luni/windows/OSMemory.c (revision 537921) +++ modules/luni/src/main/native/luni/windows/OSMemory.c (working copy) @@ -24,7 +24,7 @@ #include "IMemorySystem.h" #include "exceptions.h" -JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_malloc +JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mallocNative (JNIEnv * env, jobject thiz, jlong size) { PORT_ACCESS_FROM_ENV (env); Index: modules/luni/src/main/native/luni/windows/OSMemory.h =================================================================== --- modules/luni/src/main/native/luni/windows/OSMemory.h (revision 537921) +++ modules/luni/src/main/native/luni/windows/OSMemory.h (working copy) @@ -41,10 +41,10 @@ (JNIEnv *, jclass); /* * Class: org_apache_harmony_luni_platform_OSMemory - * Method: malloc + * Method: mallocNative * Signature: (J)J */ - JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_malloc + JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_mallocNative (JNIEnv *, jobject, jlong); /* * Class: org_apache_harmony_luni_platform_OSMemory Index: modules/portlib/src/main/native/include/shared/hyport.h =================================================================== --- modules/portlib/src/main/native/include/shared/hyport.h (revision 537921) +++ modules/portlib/src/main/native/include/shared/hyport.h (working copy) @@ -358,6 +358,12 @@ /** see @ref hysysinfo.c::hysysinfo_get_username "hysysinfo_get_username"*/ IDATA (PVMCALL sysinfo_get_username) (struct HyPortLibrary * portLibrary, char *buffer, UDATA length); + /** see @ref hysysinfo.c::hysysinfo_is_system_physical_memory_low_default "hysysinfo_is_system_physical_memory_low_default"*/ + BOOLEAN (PVMCALL sysinfo_is_system_physical_memory_low_default) (struct HyPortLibrary * portLibrary); + /** see @ref hysysinfo.c::hysysinfo_is_system_physical_memory_low "hysysinfo_is_system_physical_memory_low"*/ + BOOLEAN (PVMCALL sysinfo_is_system_physical_memory_low) (struct HyPortLibrary * portLibrary, + UDATA load_threshold, + UDATA free_threshold); /** see @ref hyfile.c::hyfile_startup "hyfile_startup"*/ I_32 (PVMCALL file_startup) (struct HyPortLibrary * portLibrary); /** see @ref hyfile.c::hyfile_shutdown "hyfile_shutdown"*/ @@ -1235,6 +1241,8 @@ #define hysysinfo_get_executable_name(param1,param2) privatePortLibrary->sysinfo_get_executable_name(privatePortLibrary,param1,param2) #define hysysinfo_get_number_CPUs() privatePortLibrary->sysinfo_get_number_CPUs(privatePortLibrary) #define hysysinfo_get_username(param1,param2) privatePortLibrary->sysinfo_get_username(privatePortLibrary,param1,param2) +#define hysysinfo_is_system_physical_memory_low_default() privatePortLibrary->sysinfo_is_system_physical_memory_low_default(privatePortLibrary) +#define hysysinfo_is_system_physical_memory_low(param1,param2) privatePortLibrary->sysinfo_is_system_physical_memory_low(privatePortLibrary,param1,param2) #define hyfile_startup() privatePortLibrary->file_startup(privatePortLibrary) #define hyfile_shutdown() privatePortLibrary->file_shutdown(privatePortLibrary) #define hyfile_write(param1,param2,param3) privatePortLibrary->file_write(privatePortLibrary,param1,param2,param3) Index: modules/portlib/src/main/native/port/shared/portpriv.h =================================================================== --- modules/portlib/src/main/native/port/shared/portpriv.h (revision 537921) +++ modules/portlib/src/main/native/port/shared/portpriv.h (working copy) @@ -583,6 +583,16 @@ hysysinfo_get_env PROTOTYPE ((struct HyPortLibrary * portLibrary, char *envVar, char *infoString, UDATA bufSize)); +struct HyPortLibrary; +extern HY_CFUNC BOOLEAN VMCALL + hysysinfo_is_system_physical_memory_low_default +PROTOTYPE ((struct HyPortLibrary * portLibrary)); +struct HyPortLibrary; +extern HY_CFUNC BOOLEAN VMCALL + hysysinfo_is_system_physical_memory_low +PROTOTYPE ((struct HyPortLibrary * portLibrary, + UDATA load_threshold, + UDATA free_threshold)); #if !defined(HY_NO_SIG) /* HySourceHySignal*/ struct HyPortLibrary; @@ -1209,6 +1219,8 @@ hysysinfo_get_executable_name, /* sysinfo_get_executable_name */ hysysinfo_get_number_CPUs, /* sysinfo_get_number_CPUs */ hysysinfo_get_username, /* sysinfo_get_username */ + hysysinfo_is_system_physical_memory_low_default, /* sysinfo_is_system_physical_memory_low_default */ + hysysinfo_is_system_physical_memory_low, /* sysinfo_is_system_physical_memory_low */ hyfile_startup, /* file_startup */ hyfile_shutdown, /* file_shutdown */ hyfile_write, /* file_write */ Index: modules/portlib/src/main/native/port/unix/hysysinfo.c =================================================================== --- modules/portlib/src/main/native/port/unix/hysysinfo.c (revision 537921) +++ modules/portlib/src/main/native/port/unix/hysysinfo.c (working copy) @@ -85,6 +85,12 @@ IDATA VMCALL hysysinfo_get_env (struct HyPortLibrary *portLibrary, char *envVar, char *infoString, UDATA bufSize); +BOOLEAN VMCALL hysysinfo_is_system_physical_memory_low_default ( + struct HyPortLibrary *portLibrary); +BOOLEAN VMCALL hysysinfo_is_system_physical_memory_low ( + struct HyPortLibrary *portLibrary, + UDATA load_threshold, + UDATA free_threshold); #undef CDEV_CURRENT_FUNCTION @@ -869,3 +875,58 @@ } #undef CDEV_CURRENT_FUNCTION + +// Default thresholds. +#define MEMORYLOADTHRESHOLD 95 // 95% +#define MEMORYFREETHRESHOLD 0x4000000 //64M + +#define CDEV_CURRENT_FUNCTION hysysinfo_is_system_physical_memory_low_default +/** + * Returns 1 if the remaining physical free memory is less + * than the default thresholds and 0 otherwise. + * + * @param[in] portLibrary The port library. + * + * @return 1 if the remaining physical free memory is less + * than the default thresholds and 0 otherwise + */ +BOOLEAN VMCALL +hysysinfo_is_system_physical_memory_low_default (struct HyPortLibrary *portLibrary) +{ + return hysysinfo_is_system_physical_memory_low(portLibrary, MEMORYLOADTHRESHOLD, MEMORYFREETHRESHOLD); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hysysinfo_is_system_physical_memory_low +/** + * Returns 1 if the remaining physical free memory is less + * than the given thresholds and 0 otherwise. + * + * @param[in] portLibrary The port library. + * @param[in] load_threshold Used memory threshold (in %) + * @param[in] free_threshold Available memory threshold + * + * @return 1 if the remaining physical free memory is less + * than the given thresholds and 0 otherwise. + */ +BOOLEAN VMCALL +hysysinfo_is_system_physical_memory_low (struct HyPortLibrary *portLibrary, + UDATA load_threshold, UDATA free_threshold) +{ + struct sysinfo info; + int memoryLoad = 100; + + if(0 == sysinfo(&info)) + { + memoryLoad = 100 - ((float)info.freeram/info.totalram) * 100; + + if(memoryLoad >= load_threshold || (info.freeram * info.mem_unit) <= free_threshold) + { + return TRUE; + } + } + return FALSE; +} + +#undef CDEV_CURRENT_FUNCTION Index: modules/portlib/src/main/native/port/windows/hysysinfo.c =================================================================== --- modules/portlib/src/main/native/port/windows/hysysinfo.c (revision 537921) +++ modules/portlib/src/main/native/port/windows/hysysinfo.c (working copy) @@ -64,6 +64,12 @@ IDATA VMCALL hysysinfo_get_env (struct HyPortLibrary *portLibrary, char *envVar, char *infoString, UDATA bufSize); +BOOLEAN VMCALL hysysinfo_is_system_physical_memory_low_default ( + struct HyPortLibrary *portLibrary); +BOOLEAN VMCALL hysysinfo_is_system_physical_memory_low ( + struct HyPortLibrary *portLibrary, + UDATA load_threshold, + UDATA free_threshold); #undef CDEV_CURRENT_FUNCTION #define CDEV_CURRENT_FUNCTION hysysinfo_get_CPU_architecture @@ -601,3 +607,57 @@ } #undef CDEV_CURRENT_FUNCTION + +// Default thresholds. +#define MEMORYLOADTHRESHOLD 95 // 95% +#define MEMORYFREETHRESHOLD 0x4000000 //64M + +#define CDEV_CURRENT_FUNCTION hysysinfo_is_system_physical_memory_low_default +/** + * Returns 1 if the remaining physical free memory is less + * than the default thresholds and 0 otherwise. + * + * @param[in] portLibrary The port library. + * + * @return 1 if the remaining physical free memory is less + * than the default thresholds and 0 otherwise + */ +BOOLEAN VMCALL +hysysinfo_is_system_physical_memory_low_default (struct HyPortLibrary *portLibrary) +{ + return hysysinfo_is_system_physical_memory_low(portLibrary, MEMORYLOADTHRESHOLD, MEMORYFREETHRESHOLD); +} + +#undef CDEV_CURRENT_FUNCTION + +#define CDEV_CURRENT_FUNCTION hysysinfo_is_system_physical_memory_low +/** + * Returns 1 if the remaining physical free memory is less + * than the given thresholds and 0 otherwise. + * + * @param[in] portLibrary The port library. + * @param[in] load_threshold Used memory threshold (in %) + * @param[in] free_threshold Available memory threshold + * + * @return 1 if the remaining physical free memory is less + * than the given thresholds and 0 otherwise. + */ +BOOLEAN VMCALL +hysysinfo_is_system_physical_memory_low (struct HyPortLibrary *portLibrary, + UDATA load_threshold, UDATA free_threshold) +{ + MEMORYSTATUSEX stat; + stat.dwLength = sizeof (stat); + + if(GlobalMemoryStatusEx(&stat)) + { + if(stat.dwMemoryLoad >= load_threshold || stat.ullAvailPhys <= load_threshold){ + return TRUE; + } + } else { + printf("GlobalMemoryStatusEx returned %d error\n", GetLastError()); + } + return FALSE; +} + +#undef CDEV_CURRENT_FUNCTION