P deallocator(Deallocator deallocator) { this.deallocator = r; if (referenceQueue != null) synchronized (DeallocatorThread.class) { int count = 0; - long lastPhysicalBytes = maxPhysicalBytes > 0 ? physicalBytes() : 0; + long lastPhysicalBytes = 0; try { + if (maxPhysicalBytes > 0) { + try { + lastPhysicalBytes = physicalBytesInaccurate(maxPhysicalBytes); + } catch (UnsatisfiedLinkError e) { + // old binaries with physicalBytesInaccurate() missing -> call physicalBytes() for backward compatibility + if (logger.isDebugEnabled()) { + logger.debug(e.getMessage()); + } + lastPhysicalBytes = physicalBytes(); + } + } while (count++ < maxRetries && ((maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes) || (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes))) { if (logger.isDebugEnabled()) { diff --git a/src/main/java/org/bytedeco/javacpp/tools/ClearMojo.java b/src/main/java/org/bytedeco/javacpp/tools/ClearMojo.java index 06f44851b..5629a40ee 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/ClearMojo.java +++ b/src/main/java/org/bytedeco/javacpp/tools/ClearMojo.java @@ -34,7 +34,7 @@ * * @author Samuel Audet */ -@Mojo(name = "clear", defaultPhase = LifecyclePhase.NONE) +@Mojo(name = "clear", defaultPhase = LifecyclePhase.NONE, requiresProject = false) public class ClearMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { try { diff --git a/src/main/java/org/bytedeco/javacpp/tools/Generator.java b/src/main/java/org/bytedeco/javacpp/tools/Generator.java index b46c7b15f..75e547c36 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Generator.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Generator.java @@ -543,7 +543,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver out.println("#endif"); out.println("}"); out.println(); - out.println("static inline jlong JavaCPP_physicalBytes() {"); + out.println("static inline jlong JavaCPP_physicalBytes(jlong maxSize = 0) {"); out.println(" jlong size = 0;"); out.println("#ifdef __linux__"); out.println(" static int fd = open(\"/proc/self/statm\", O_RDONLY, 0);"); @@ -567,6 +567,11 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver out.println(" size = (jlong)info.internal;"); out.println(" }"); out.println("#elif defined(_WIN32)"); + out.println(" PROCESS_MEMORY_COUNTERS counters;"); + out.println(" if (GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters))) {"); + out.println(" jlong size2 = (jlong)counters.WorkingSetSize;"); + out.println(" if (size2 <= maxSize) return size2;"); + out.println(" }"); out.println(" DWORD length = sizeof(PSAPI_WORKING_SET_INFORMATION);"); out.println(" PSAPI_WORKING_SET_INFORMATION *info = (PSAPI_WORKING_SET_INFORMATION*)malloc(length);"); out.println(" BOOL success = QueryWorkingSet(GetCurrentProcess(), info, length);"); diff --git a/src/test/java/org/bytedeco/javacpp/PointerTest.java b/src/test/java/org/bytedeco/javacpp/PointerTest.java index d4ee0ff51..90fd067a2 100644 --- a/src/test/java/org/bytedeco/javacpp/PointerTest.java +++ b/src/test/java/org/bytedeco/javacpp/PointerTest.java @@ -1172,6 +1172,6 @@ static class TestFunction extends FunctionPointer { long bytesAfter = Pointer.physicalBytes(); System.out.println(bytesBefore + " " + bytesAfter); - assertTrue(Math.abs(bytesAfter - bytesBefore) < 10_000_000 + buffer.get(0)); + assertTrue(Math.abs(bytesAfter - bytesBefore) < 100_000_000 + buffer.get(0)); } }