Skip to content

Commit

Permalink
Added Linux implementation of method returning free memory even with …
Browse files Browse the repository at this point in the history
…cached and buffer values.
  • Loading branch information
Martin Hanyáš committed May 22, 2017
1 parent 64cb8e6 commit 869bad1
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/jezhumble/javasysmon/JavaSysMon.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public MemoryStats physical() {
return monitor.physical();
}

/**
* Gets the physical memory installed, and the amount free + buffers/cache.
*
* @return An object containing the amount of physical
* memory installed, and the amount free + buffers/cache.
*/
public MemoryStats physicalWithBuffersAndCached() {
return monitor.physicalWithBuffersAndCached();
}

/**
* Gets the amount of swap available to the operating system,
* and the amount that is free.
Expand Down
32 changes: 24 additions & 8 deletions src/main/java/com/jezhumble/javasysmon/LinuxMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class LinuxMonitor implements Monitor {
Pattern.compile("MemTotal:\\s+(\\d+) kB", Pattern.MULTILINE);
private static final Pattern FREE_MEMORY_PATTERN =
Pattern.compile("MemFree:\\s+(\\d+) kB", Pattern.MULTILINE);
private static final Pattern BUFFERS_PATTERN =
Pattern.compile("Buffers:\\s+(\\d+) kB", Pattern.MULTILINE);
private static final Pattern CACHED_PATTERN =
Pattern.compile("Cached:\\s+(\\d+) kB", Pattern.MULTILINE);
private static final Pattern TOTAL_SWAP_PATTERN =
Pattern.compile("SwapTotal:\\s+(\\d+) kB", Pattern.MULTILINE);
private static final Pattern FREE_SWAP_PATTERN =
Expand All @@ -35,6 +39,8 @@ class LinuxMonitor implements Monitor {
Pattern.compile("([\\d]*).*");
private static final Pattern DISTRIBUTION =
Pattern.compile("DISTRIB_DESCRIPTION=\"(.*)\"", Pattern.MULTILINE);
public static final String PROC_MEMINFO = "/proc/meminfo";
public static final int TO_BYTE = 1024;

private FileUtils fileUtils;
private int userHz = 100; // Shouldn't be hardcoded. See below.
Expand Down Expand Up @@ -66,18 +72,28 @@ public String osName() {
}

public MemoryStats physical() {
String totalMemory = fileUtils.runRegexOnFile(TOTAL_MEMORY_PATTERN, "/proc/meminfo");
long total = Long.parseLong(totalMemory) * 1024;
String freeMemory = fileUtils.runRegexOnFile(FREE_MEMORY_PATTERN, "/proc/meminfo");
long free = Long.parseLong(freeMemory) * 1024;
String totalMemory = fileUtils.runRegexOnFile(TOTAL_MEMORY_PATTERN, PROC_MEMINFO);
long total = Long.parseLong(totalMemory) * TO_BYTE;
String freeMemory = fileUtils.runRegexOnFile(FREE_MEMORY_PATTERN, PROC_MEMINFO);
long free = Long.parseLong(freeMemory) * TO_BYTE;
return new MemoryStats(free, total);
}

public MemoryStats physicalWithBuffersAndCached() {
String totalMemory = fileUtils.runRegexOnFile(TOTAL_MEMORY_PATTERN, PROC_MEMINFO);
long total = Long.parseLong(totalMemory) * TO_BYTE;
String freeMemory = fileUtils.runRegexOnFile(FREE_MEMORY_PATTERN, PROC_MEMINFO);
String buffers = fileUtils.runRegexOnFile(BUFFERS_PATTERN, PROC_MEMINFO);
String cached = fileUtils.runRegexOnFile(CACHED_PATTERN, PROC_MEMINFO);
long free = (Long.parseLong(freeMemory) + Long.parseLong(buffers) + Long.parseLong(cached)) * TO_BYTE;
return new MemoryStats(free, total);
}

public MemoryStats swap() {
String totalMemory = fileUtils.runRegexOnFile(TOTAL_SWAP_PATTERN, "/proc/meminfo");
long total = Long.parseLong(totalMemory) * 1024;
String freeMemory = fileUtils.runRegexOnFile(FREE_SWAP_PATTERN, "/proc/meminfo");
long free = Long.parseLong(freeMemory) * 1024;
String totalMemory = fileUtils.runRegexOnFile(TOTAL_SWAP_PATTERN, PROC_MEMINFO);
long total = Long.parseLong(totalMemory) * TO_BYTE;
String freeMemory = fileUtils.runRegexOnFile(FREE_SWAP_PATTERN, PROC_MEMINFO);
long free = Long.parseLong(freeMemory) * TO_BYTE;
return new MemoryStats(free, total);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/jezhumble/javasysmon/MacOsXMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public String osName() {
public native int currentPid();
public native CpuTimes cpuTimes();
public native MemoryStats physical();
public MemoryStats physicalWithBuffersAndCached(){ throw new UnsupportedOperationException("No implementation for " + osName()); }
public native MemoryStats swap();
public native ProcessInfo[] processTable();
public native void killProcess(int pid);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/jezhumble/javasysmon/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public interface Monitor {
*/
public MemoryStats physical();

/**
* Gets the physical memory installed, and the amount free + buffers/cache.
*
* @return An object containing the amount of physical
* memory installed, and the amount free + buffers/cache.
*/
public MemoryStats physicalWithBuffersAndCached();

/**
* Gets the amount of swap available to the operating system,
* and the amount that is free.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/jezhumble/javasysmon/NullMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public MemoryStats physical() {
return null;
}

public MemoryStats physicalWithBuffersAndCached(){
return null;
}

public MemoryStats swap() {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/jezhumble/javasysmon/SolarisMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public ProcessInfo[] processTable() {
public native int currentPid();
public native CpuTimes cpuTimes();
public native MemoryStats physical();
public MemoryStats physicalWithBuffersAndCached(){ throw new UnsupportedOperationException("No implementation for " + osName()); }
public native MemoryStats swap();
public native void killProcess(int pid);
}
1 change: 1 addition & 0 deletions src/main/java/com/jezhumble/javasysmon/WindowsMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public String osName() {
public native long uptimeInSeconds();
public native CpuTimes cpuTimes();
public native MemoryStats physical();
public MemoryStats physicalWithBuffersAndCached(){ throw new UnsupportedOperationException("No implementation for " + osName()); }
public native MemoryStats swap();
public native ProcessInfo[] processTable();
public native void killProcess(int pid);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/jezhumble/javasysmon/LinuxMonitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public void testShouldRetrieveFreeMemory() {
Assert.assertEquals((long)195608, freeMemory/1024);
}

public void testShouldRetrieveFreeMemoryWithCachedAndBuffer() {
LinuxMonitor monitor = new LinuxMonitor(new StubFileUtils());
final long freeMemory = monitor.physicalWithBuffersAndCached().getFreeBytes();
Assert.assertEquals((long)196038, freeMemory/1024);
}

public void testShouldRetrieveTotalSwap() {
LinuxMonitor monitor = new LinuxMonitor(new StubFileUtils());
final long totalSwap = monitor.swap().getTotalBytes();
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/test_meminfo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MemTotal: 368640 kB
MemFree: 195608 kB
Buffers: 0 kB
Cached: 0 kB
Buffers: 100 kB
Cached: 330 kB
SwapCached: 0 kB
Active: 0 kB
Inactive: 0 kB
Expand Down

0 comments on commit 869bad1

Please sign in to comment.