-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SystemMemoryInfo.java
67 lines (60 loc) · 2.15 KB
/
SystemMemoryInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package azkaban.utils;
import javax.inject.Inject;
import org.slf4j.LoggerFactory;
/**
* This class is used to maintain system memory information. Processes utilizing large amount of
* memory should consult this class to see if the system has enough memory to proceed the
* operation.
*
* Memory information is obtained from /proc/meminfo, so only Unix/Linux like system will support
* this class.
*
* All the memory size used in this function is in KB.
*/
public class SystemMemoryInfo {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemMemoryInfo.class);
private static final long LOW_MEM_THRESHOLD = 3L * 1024L * 1024L; //3 GB
private final OsMemoryUtil util;
@Inject
public SystemMemoryInfo(final OsMemoryUtil util) {
this.util = util;
}
/**
* @param xmx Xmx for the process
* @return true if the system can satisfy the memory request
*
* Given Xmx value (in kb) used by java process, determine if system can satisfy the memory
* request.
*/
public boolean canSystemGrantMemory(final long xmx) {
final long freeMemSize = this.util.getOsTotalFreeMemorySize();
if (freeMemSize == 0) {
// Fail open.
// On the platforms that don't support the mem info file, the returned size will be 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;
}
/**
* @param memKb represents a memory value in kb
* @return true if available physical memory is greater than memKb
*
* Verifies if the currently available physical memory is greater than a given value.
*/
public boolean isFreePhysicalMemoryAbove(final long memKb) {
final long freeMemSize = this.util.getOsFreePhysicalMemorySize();
if (freeMemSize == 0) {
// Fail open.
// On the platforms that don't support the mem info file, the returned size will be 0.
return true;
}
return freeMemSize - memKb > 0;
}
}