Skip to content

Commit d6443b7

Browse files
committed
BoxStore: add Linux-only sysProcMeminfoKb() and sysProcStatusKb()
1 parent fd6c29b commit d6443b7

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ static native void nativeRegisterCustomType(long store, int entityId, int proper
182182

183183
native long nativeValidate(long store, long pageLimit, boolean checkLeafLevel);
184184

185+
static native long nativeSysProcMeminfoKb(String key);
186+
187+
static native long nativeSysProcStatusKb(String key);
188+
185189
/** @return sync availability (0: none; 1: client; 2: server) */
186190
static native int nativeGetSupportedSync();
187191

@@ -405,7 +409,6 @@ public static boolean isDatabaseOpen(Object context, @Nullable String dbNameOrNu
405409
*/
406410
public static boolean isDatabaseOpen(@Nullable File baseDirectoryOrNull,
407411
@Nullable String dbNameOrNull) throws IOException {
408-
409412
File dbDir = BoxStoreBuilder.getDbDir(baseDirectoryOrNull, dbNameOrNull);
410413
return isFileOpen(dbDir.getCanonicalPath());
411414
}
@@ -420,6 +423,43 @@ public static boolean isDatabaseOpen(File directory) throws IOException {
420423
return isFileOpen(directory.getCanonicalPath());
421424
}
422425

426+
/**
427+
* Linux only: extracts a kB value from /proc/meminfo (system wide memory information).
428+
* A couple of interesting keys (from 'man proc'):
429+
* - MemTotal: Total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code).
430+
* - MemFree: The sum of LowFree+HighFree.
431+
* - MemAvailable: An estimate of how much memory is available for starting new applications, without swapping.
432+
*
433+
* @param key The string identifying the wanted line from /proc/meminfo to extract a Kb value from. E.g. "MemTotal".
434+
* @return Kb value or 0 on failure
435+
*/
436+
@Experimental
437+
static long sysProcMeminfoKb(String key) {
438+
return nativeSysProcMeminfoKb(key);
439+
}
440+
441+
/**
442+
* Linux only: extracts a kB value from /proc/self/status (process specific information).
443+
* A couple of interesting keys (from 'man proc'):
444+
* - VmPeak: Peak virtual memory size.
445+
* - VmSize: Virtual memory size.
446+
* - VmHWM: Peak resident set size ("high water mark").
447+
* - VmRSS: Resident set size. Note that the value here is the sum of RssAnon, RssFile, and RssShmem.
448+
* - RssAnon: Size of resident anonymous memory. (since Linux 4.5).
449+
* - RssFile: Size of resident file mappings. (since Linux 4.5).
450+
* - RssShmem: Size of resident shared memory (includes System V shared memory, mappings from tmpfs(5),
451+
* and shared anonymous mappings). (since Linux 4.5).
452+
* - VmData, VmStk, VmExe: Size of data, stack, and text segments.
453+
* - VmLib: Shared library code size.
454+
*
455+
* @param key The string identifying the wanted line from /proc/self/status to extract a Kb value from. E.g. "VmSize".
456+
* @return Kb value or 0 on failure
457+
*/
458+
@Experimental
459+
static long sysProcStatusKb(String key) {
460+
return nativeSysProcStatusKb(key);
461+
}
462+
423463
/**
424464
* The size in bytes occupied by the data file on disk.
425465
*

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.Test;
2020

2121
import java.io.File;
22+
import java.util.Locale;
2223
import java.util.concurrent.Callable;
2324

2425
import javax.annotation.Nullable;
@@ -240,4 +241,21 @@ public void testIsObjectBrowserAvailable() {
240241
assertFalse(BoxStore.isObjectBrowserAvailable());
241242
}
242243

244+
@Test
245+
public void testSysProc() {
246+
long vmRss = BoxStore.sysProcStatusKb("VmRSS");
247+
long memAvailable = BoxStore.sysProcMeminfoKb("MemAvailable");
248+
249+
final String osName = System.getProperty("os.name");
250+
if (osName.toLowerCase().contains("linux")) {
251+
System.out.println("VmRSS: " + vmRss);
252+
System.out.println("MemAvailable: " + memAvailable);
253+
assertTrue(vmRss > 0);
254+
assertTrue(memAvailable > 0);
255+
} else {
256+
assertEquals(vmRss, 0);
257+
assertEquals(memAvailable, 0);
258+
}
259+
}
260+
243261
}

0 commit comments

Comments
 (0)