SystemMemoryInfo { public boolean canSystemGrantMemory(final long xmx) { final long freeMemSize = this.util.getOsTotalFreeMemorySize(); if (freeMemSize == 0) { return true; } if (freeMemSize - xmx < LOW_MEM_THRESHOLD) { logger.info(String.format( "Free memory amount minus Xmx (%d - %d kb) is less than low mem threshold (%d kb), " + "memory request declined.", freeMemSize, xmx, LOW_MEM_THRESHOLD)); return false; } return true; } @Inject  SystemMemoryInfo(final OsMemoryUtil util); boolean canSystemGrantMemory(final long xmx); boolean isFreePhysicalMemoryAbove(final long memKb);  }
@Test public void grantedIfFreeMemoryAvailable() throws Exception { final OsMemoryUtil memUtil = mock(OsMemoryUtil.class); final long availableFreeMem = 10L * 1024L * 1024L; when(memUtil.getOsTotalFreeMemorySize()).thenReturn(availableFreeMem); final SystemMemoryInfo memInfo = new SystemMemoryInfo(memUtil); final boolean isGranted = memInfo.canSystemGrantMemory(1); assertTrue(isGranted); }
@Test public void notGrantedIfFreeMemoryAvailableLessThanMinimal() throws Exception { final OsMemoryUtil memUtil = mock(OsMemoryUtil.class); final long availableFreeMem = 4L * 1024L * 1024L; when(memUtil.getOsTotalFreeMemorySize()).thenReturn(availableFreeMem); final SystemMemoryInfo memInfo = new SystemMemoryInfo(memUtil); final long xmx = 2 * GB_UNIT; final boolean isGranted = memInfo.canSystemGrantMemory(xmx); assertFalse(isGranted); }
@Test public void grantedIfFreeMemoryCheckReturnsZero() throws Exception { final OsMemoryUtil memUtil = mock(OsMemoryUtil.class); final long availableFreeMem = 0; when(memUtil.getOsTotalFreeMemorySize()).thenReturn(availableFreeMem); final SystemMemoryInfo memInfo = new SystemMemoryInfo(memUtil); final long xmx = 0; final boolean isGranted = memInfo.canSystemGrantMemory(xmx); assertTrue("Memory check failed. Should fail open", isGranted); }