1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.util;
17
18 import static org.apache.commons.io.FileUtils.ONE_EB_BI;
19 import static org.apache.commons.io.FileUtils.ONE_GB_BI;
20 import static org.apache.commons.io.FileUtils.ONE_KB_BI;
21 import static org.apache.commons.io.FileUtils.ONE_MB_BI;
22 import static org.apache.commons.io.FileUtils.ONE_PB_BI;
23 import static org.apache.commons.io.FileUtils.ONE_TB_BI;
24
25 import java.math.BigDecimal;
26 import java.math.BigInteger;
27 import java.time.LocalDateTime;
28 import java.time.ZonedDateTime;
29 import java.util.Collection;
30 import java.util.Date;
31 import java.util.Map;
32 import java.util.Objects;
33
34
35
36
37 public final class MemoryUtil {
38 private MemoryUtil() {
39 }
40
41
42
43
44
45
46 public static String getMemoryUsageLog() {
47 final Runtime runtime = Runtime.getRuntime();
48 final long freeBytes = runtime.freeMemory();
49 final long maxBytes = runtime.maxMemory();
50 final long totalBytes = runtime.totalMemory();
51 final long usedBytes = totalBytes - freeBytes;
52 return "Mem:{used " + byteCountToDisplaySize(usedBytes) + ", heap " + byteCountToDisplaySize(totalBytes) + ", max "
53 + byteCountToDisplaySize(maxBytes) + "}";
54 }
55
56
57
58
59
60
61
62 public static String byteCountToDisplaySize(final long size) {
63 return byteCountToDisplaySize(BigInteger.valueOf(size));
64 }
65
66 private static String byteCountToDisplaySize(final BigInteger size) {
67 Objects.requireNonNull(size, "size");
68 final String displaySize;
69
70 if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
71 displaySize = new BigDecimal(size.divide(ONE_PB_BI)).divide(BigDecimal.valueOf(1000)) + "EB";
72 } else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
73 displaySize = new BigDecimal(size.divide(ONE_TB_BI)).divide(BigDecimal.valueOf(1000)) + "PB";
74 } else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
75 displaySize = new BigDecimal(size.divide(ONE_GB_BI)).divide(BigDecimal.valueOf(1000)) + "TB";
76 } else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
77 displaySize = new BigDecimal(size.divide(ONE_MB_BI)).divide(BigDecimal.valueOf(1000)) + "GB";
78 } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
79 displaySize = new BigDecimal(size.divide(ONE_KB_BI)).divide(BigDecimal.valueOf(1000)) + "MB";
80 } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
81 displaySize = new BigDecimal(size).divide(BigDecimal.valueOf(1000)) + "KB";
82 } else {
83 displaySize = size + "bytes";
84 }
85 return displaySize;
86 }
87
88
89
90
91
92
93 public static long getUsedMemory() {
94 final Runtime runtime = Runtime.getRuntime();
95 final long freeBytes = runtime.freeMemory();
96 final long totalBytes = runtime.totalMemory();
97 return totalBytes - freeBytes;
98 }
99
100
101
102
103
104
105
106 public static long sizeOf(final Object obj) {
107 if (obj == null) {
108 return 0L;
109 }
110 if (obj instanceof String) {
111 return ((String) obj).length() + 56L;
112 }
113 if (obj instanceof Number) {
114 return 24L;
115 }
116 if (obj instanceof Date) {
117 return 32L;
118 }
119 if (obj instanceof LocalDateTime) {
120 return 80L;
121 }
122 if (obj instanceof ZonedDateTime) {
123 return 2128L;
124 }
125 if (obj instanceof Object[]) {
126 long size = 0;
127 for (final Object value : (Object[]) obj) {
128 size += sizeOf(value);
129 }
130 return size;
131 }
132 if (obj instanceof Collection<?>) {
133 long size = 0;
134 for (final Object value : (Collection<?>) obj) {
135 size += sizeOf(value);
136 }
137 return size;
138 }
139 if (obj instanceof Map<?, ?>) {
140 long size = 0;
141 for (final Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
142 size += sizeOf(entry.getKey());
143 size += sizeOf(entry.getValue());
144 }
145 return size;
146 }
147 return 16L;
148 }
149 }