View Javadoc
1   /*
2    * Copyright 2012-2025 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 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   * Utility class for memory operations and size calculations.
36   */
37  public final class MemoryUtil {
38      private MemoryUtil() {
39      }
40  
41      /**
42       * Gets a formatted memory usage log string.
43       *
44       * @return formatted memory usage information
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       * Converts byte count to human-readable size format.
58       *
59       * @param size the size in bytes
60       * @return formatted size string
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       * Gets the currently used memory in bytes.
90       *
91       * @return used memory in bytes
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      * Estimates the size of an object in bytes.
102      *
103      * @param obj the object to calculate size for
104      * @return estimated size in bytes
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 }