planetiler/planetiler-core/src/main/java/com/onthegomap/planetiler/util/MemoryEstimator.java

88 wiersze
2.2 KiB
Java
Czysty Zwykły widok Historia

package com.onthegomap.planetiler.util;
2021-05-04 12:02:22 +00:00
2022-03-01 13:43:19 +00:00
import com.carrotsearch.hppc.Accountable;
2021-05-04 12:02:22 +00:00
2021-09-10 00:46:20 +00:00
/**
* Utilities to estimate the size of in-memory objects.
*/
@SuppressWarnings("SameReturnValue")
2021-05-04 12:02:22 +00:00
public class MemoryEstimator {
2021-09-10 00:46:20 +00:00
public static final long CLASS_HEADER_BYTES = 24L;
public static final long INT_BYTES = 4L;
public static final long LONG_BYTES = 8L;
public static final long POINTER_BYTES = 8L;
public static long estimateSize(HasEstimate object) {
2021-06-05 12:02:51 +00:00
return object == null ? 0 : object.estimateMemoryUsageBytes();
2021-05-04 12:02:22 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateSize(String string) {
return string == null ? 0 : (54 + string.getBytes().length);
}
public static long estimateSizeInt(int ignored) {
return INT_BYTES;
}
public static long estimateSizeLong(long ignored) {
return LONG_BYTES;
}
public static long estimateSize(byte ignored) {
return 1;
}
public static long estimateSize(boolean ignored) {
return 1;
2021-05-04 12:02:22 +00:00
}
2021-09-10 00:46:20 +00:00
/**
2022-03-01 13:43:19 +00:00
* Estimates the size of an HPPC {@link Accountable} instance.
2021-09-10 00:46:20 +00:00
*/
2022-03-01 13:43:19 +00:00
public static long estimateSize(Accountable object) {
return object == null ? 0 : object.ramBytesAllocated();
2021-05-04 12:02:22 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateArraySize(int length, long entrySize) {
return CLASS_HEADER_BYTES + entrySize * length;
2021-05-04 12:02:22 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateByteArraySize(int length) {
return estimateArraySize(length, 1);
}
2021-09-10 00:46:20 +00:00
public static long estimateIntArraySize(int length) {
return estimateArraySize(length, INT_BYTES);
}
2021-09-10 00:46:20 +00:00
public static long estimateLongArraySize(int length) {
return estimateArraySize(length, LONG_BYTES);
2021-06-18 11:21:43 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateObjectArraySize(int length) {
return estimateArraySize(length, POINTER_BYTES);
2021-06-18 11:21:43 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateSize(byte[] array) {
return estimateByteArraySize(array.length);
2021-06-18 11:21:43 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateSize(int[] array) {
return estimateIntArraySize(array.length);
2021-07-24 00:20:51 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateSize(long[] array) {
return estimateLongArraySize(array.length);
2021-07-24 00:20:51 +00:00
}
2021-09-10 00:46:20 +00:00
public static long estimateSize(Object[] array) {
return estimateObjectArraySize(array.length);
2021-07-24 00:20:51 +00:00
}
2021-05-04 12:02:22 +00:00
public interface HasEstimate {
long estimateMemoryUsageBytes();
}
}