Index: vm/port/include/port_vmem.h =================================================================== --- vm/port/include/port_vmem.h (revision 527067) +++ vm/port/include/port_vmem.h (working copy) @@ -136,22 +136,26 @@ APR_DECLARE(size_t *) port_vmem_page_sizes(); /** - * Returns the amount of currently used memory in bytes. + * Returns the amount of currently used memory in bytes + * or 0 if this value could not be calculated. */ APR_DECLARE(size_t) port_vmem_used_size(); /** - * Returns the amount of committed memory in bytes. + * Returns the amount of committed memory in bytes + * or 0 if this value could not be calculated. */ APR_DECLARE(size_t) port_vmem_committed_size(); /** - * Returns the amount of reserved memory in bytes. + * Returns the amount of reserved memory in bytes + * or 0 if this value could not be calculated. */ APR_DECLARE(size_t) port_vmem_reserved_size(); /** - * Returns the maximum amount of memory which could be reserved in bytes. + * Returns the maximum amount of memory which could be reserved in bytes + * or 0 if this value could not be calculated */ APR_DECLARE(size_t) port_vmem_max_size(); Index: vm/port/src/vmem/linux/port_vmem.c =================================================================== --- vm/port/src/vmem/linux/port_vmem.c (revision 527067) +++ vm/port/src/vmem/linux/port_vmem.c (working copy) @@ -144,28 +144,44 @@ } APR_DECLARE(size_t) port_vmem_committed_size(){ - char buf[PATH_MAX]; + char* buf = (char*) malloc(PATH_MAX + 1); - pid_t pid = getpid(); - sprintf(buf, "/proc/%d/statm", pid); - FILE* file = fopen(buf, "rt"); + pid_t my_pid = getpid(); + sprintf(buf, "/proc/%d/statm", my_pid); + FILE* file = fopen(buf, "r"); if (!file) { - return port_vmem_page_sizes()[0]; + return 0; } + size_t size = 0; + ssize_t len = getline(&buf, &size, file); + if (len == -1) { + return 0; + } size_t vmem; int res = sscanf(buf, "%lu", &vmem); + if (res < 1) { + return 0; + } + if (buf) { + free(buf); + } return vmem * port_vmem_page_sizes()[0]; } APR_DECLARE(size_t) port_vmem_max_size(){ - char buf[PATH_MAX]; + char* buf = (char*) malloc(PATH_MAX + 1); - pid_t mypid = getpid(); - sprintf(buf, "/proc/%d/stat", mypid); - FILE* file = fopen(buf, "rt"); + pid_t my_pid = getpid(); + sprintf(buf, "/proc/%d/stat", my_pid); + FILE* file = fopen(buf, "r"); if (!file) { - return UINT_MAX; + return 0; } + size_t size = 0; + ssize_t len = getline(&buf, &size, file); + if (len == -1) { + return 0; + } int pid, ppid, pgrp, session, tty_nr, tpgid, exit_signal, processor; char comm[PATH_MAX]; char state; @@ -183,7 +199,7 @@ &startcode, &endcode, &startstack, &kstkesp, &kstkeip, &signal, &blocked, &sigignore, &sigcatch, &wchan, &nswap, &cnswap, &exit_signal, &processor); if (res < 25) { // rlim position - return UINT_MAX; + return 0; }; return rlim; } Index: vm/vmcore/src/kernel_classes/native/org_apache_harmony_lang_management_MemoryMXBeanImpl.cpp =================================================================== --- vm/vmcore/src/kernel_classes/native/org_apache_harmony_lang_management_MemoryMXBeanImpl.cpp (revision 527067) +++ vm/vmcore/src/kernel_classes/native/org_apache_harmony_lang_management_MemoryMXBeanImpl.cpp (working copy) @@ -117,19 +117,21 @@ - ((JavaVM_Internal*)vm)->vm_env->init_gc_used_memory; if (init <= 0) {init = -1;} - jlong used = port_vmem_used_size() - gc_total_memory(); + jlong used = port_vmem_used_size(); if (used < init) {used = init;} + if (used == -1) {used = 0;} - jlong committed = port_vmem_committed_size() - gc_total_memory(); + jlong committed = port_vmem_committed_size(); if (committed < used) {committed = used;} + if (committed == -1) {committed = 0;} - jlong max = port_vmem_max_size() - gc_total_memory(); - if (max < committed) {max = committed;} + jlong max = port_vmem_max_size(); + if ((max < committed) && (max != -1)) {max = committed;} jclass memoryUsageClazz =jenv->FindClass("java/lang/management/MemoryUsage"); - if (jenv->ExceptionCheck()) {return NULL;}; + if (jenv->ExceptionCheck()) {return NULL;} jmethodID memoryUsageClazzConstructor = jenv->GetMethodID(memoryUsageClazz, "", "(JJJJ)V"); - if (jenv->ExceptionCheck()) {return NULL;}; + if (jenv->ExceptionCheck()) {return NULL;} jobject memoryUsage = jenv->NewObject(memoryUsageClazz, memoryUsageClazzConstructor, init, used, committed, max);