Skip to content

Commit

Permalink
* Call malloc_trim(0) after System.gc() on Linux to make sure me…
Browse files Browse the repository at this point in the history
…mory gets released (issue bytedeco/javacpp-presets#423)
  • Loading branch information
saudet committed Jul 9, 2017
1 parent c4fa2c1 commit 86e4d57
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Call `malloc_trim(0)` after `System.gc()` on Linux to make sure memory gets released ([issue bytedeco/javacpp-presets#423](https://github.com/bytedeco/javacpp-presets/issues/423))
* Make public the `Pointer.formatBytes()` and `Pointer.parseBytes()` static methods
* Use `Integer.decode()` instead of `parseInt()` on integer literals to support hexadecimal and octal numbers
* Add `Builder.encoding` option to let users specify I/O character set name ([issue bytedeco/javacpp-presets#195](https://github.com/bytedeco/javacpp-presets/issues/195))
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static class DeallocatorThread extends Thread {

/** Maximum number of times to call {@link System#gc()} before giving up with {@link OutOfMemoryError}.
* Set via "org.bytedeco.javacpp.maxretries" system property, defaults to 10, where each retry is followed
* by a call to {@code Thread.sleep(100)}. */
* by a call to {@code Thread.sleep(100)} and {@code Pointer.trimMemory()}. */
static final int maxRetries;

public static String formatBytes(long bytes) {
Expand Down Expand Up @@ -430,6 +430,9 @@ public static long maxPhysicalBytes() {
return maxPhysicalBytes;
}

/** Makes sure to return freed memory to the system, as required by Linux, at least. */
@Name("JavaCPP_trimMemory") private static native boolean trimMemory();

/** Returns the amount of physical memory currently used by the whole process, or 0 if unknown.
* Also known as "resident set size" (Linux, Mac OS X, etc) or "working set size" (Windows). */
@Name("JavaCPP_physicalBytes") public static synchronized native long physicalBytes();
Expand Down Expand Up @@ -547,17 +550,18 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
while (count++ < maxRetries && ((maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes)
|| (maxPhysicalBytes > 0 && (lastPhysicalBytes = physicalBytes()) > maxPhysicalBytes))) {
if (logger.isDebugEnabled()) {
logger.debug("Calling System.gc() in " + this);
logger.debug("Calling System.gc() and Pointer.trimMemory() in " + this);
}
// try to get some more memory back
System.gc();
Thread.sleep(100);
trimMemory();
}
} catch (InterruptedException ex) {
// reset interrupt to be nice
Thread.currentThread().interrupt();
} catch (UnsatisfiedLinkError e) {
// old binaries with physicalBytes() missing -> ignore for backward compatibility
// old binaries with physicalBytes() or trimMemory() missing -> ignore for backward compatibility
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage());
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println("#endif");
out.println();
out.println("#ifdef __linux__");
out.println(" #include <malloc.h>");
out.println(" #include <sys/types.h>");
out.println(" #include <sys/stat.h>");
out.println(" #include <sys/sysinfo.h>");
Expand Down Expand Up @@ -430,6 +431,14 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" va_end(ap);");
out.println("}");
out.println();
out.println("static inline jboolean JavaCPP_trimMemory() {");
out.println("#if defined(__linux__) && !defined(__ANDROID__)");
out.println(" return (jboolean)malloc_trim(0);");
out.println("#else");
out.println(" return 0;");
out.println("#endif");
out.println("}");
out.println();
out.println("static inline jlong JavaCPP_physicalBytes() {");
out.println(" jlong size = 0;");
out.println("#ifdef __linux__");
Expand Down

0 comments on commit 86e4d57

Please sign in to comment.