View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.time.LocalDateTime;
19  import java.time.ZonedDateTime;
20  import java.util.Collection;
21  import java.util.Date;
22  import java.util.Map;
23  
24  import org.apache.commons.io.FileUtils;
25  import org.codelibs.core.lang.StringUtil;
26  
27  public final class MemoryUtil {
28      private MemoryUtil() {
29      }
30  
31      public static String getMemoryUsageLog() {
32          final Runtime runtime = Runtime.getRuntime();
33          final long freeBytes = runtime.freeMemory();
34          final long maxBytes = runtime.maxMemory();
35          final long totalBytes = runtime.totalMemory();
36          final long usedBytes = totalBytes - freeBytes;
37          return "Mem:{used " + byteCountToDisplaySize(usedBytes) + ", heap " + byteCountToDisplaySize(totalBytes) + ", max "
38                  + byteCountToDisplaySize(maxBytes) + "}";
39      }
40  
41      public static String byteCountToDisplaySize(final long size) {
42          return FileUtils.byteCountToDisplaySize(size).replace(" ", StringUtil.EMPTY);
43      }
44  
45      public static long getUsedMemory() {
46          final Runtime runtime = Runtime.getRuntime();
47          final long freeBytes = runtime.freeMemory();
48          final long totalBytes = runtime.totalMemory();
49          return totalBytes - freeBytes;
50      }
51  
52      public static long sizeOf(final Object obj) {
53          if (obj == null) {
54              return 0L;
55          }
56          if (obj instanceof String) {
57              return ((String) obj).length() + 56L;
58          }
59          if (obj instanceof Number) {
60              return 24L;
61          } else if (obj instanceof Date) {
62              return 32L;
63          } else if (obj instanceof LocalDateTime) {
64              return 80L;
65          } else if (obj instanceof ZonedDateTime) {
66              return 2128L;
67          } else if (obj instanceof Object[]) {
68              long size = 0;
69              for (final Object value : (Object[]) obj) {
70                  size += sizeOf(value);
71              }
72              return size;
73          } else if (obj instanceof Collection<?>) {
74              long size = 0;
75              for (final Object value : (Collection<?>) obj) {
76                  size += sizeOf(value);
77              }
78              return size;
79          } else if (obj instanceof Map<?, ?>) {
80              long size = 0;
81              for (final Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
82                  size += sizeOf(entry.getKey());
83                  size += sizeOf(entry.getValue());
84              }
85              return size;
86          }
87          return 16L;
88      }
89  }