| 1 | package net.minecraft.client.gui.components.debug;
|
| 2 |
|
| 3 | import java.lang.management.GarbageCollectorMXBean;
|
| 4 | import java.lang.management.ManagementFactory;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Locale;
|
| 7 | import java.util.concurrent.TimeUnit;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class DebugEntryMemory implements DebugScreenEntry {
|
| 17 | private static final Identifier GROUP = Identifier.withDefaultNamespace("memory");
|
| 18 | private final DebugEntryMemory.AllocationRateCalculator allocationRateCalculator = new DebugEntryMemory.AllocationRateCalculator();
|
| 19 |
|
| 20 | @Override
|
| 21 | public void display(
|
| 22 | final DebugScreenDisplayer displayer,
|
| 23 | final @Nullable Level serverOrClientLevel,
|
| 24 | final @Nullable LevelChunk clientChunk,
|
| 25 | final @Nullable LevelChunk serverChunk
|
| 26 | ) {
|
| 27 | long max = Runtime.getRuntime().maxMemory();
|
| 28 | long total = Runtime.getRuntime().totalMemory();
|
| 29 | long free = Runtime.getRuntime().freeMemory();
|
| 30 | long used = total - free;
|
| 31 | displayer.addToGroup(
|
| 32 | GROUP,
|
| 33 | List.of(
|
| 34 | String.format(Locale.ROOT, "Mem: %2d%% %03d/%03dMiB", used * 100L / max, bytesToMebibytes(used), bytesToMebibytes(max)),
|
| 35 | String.format(Locale.ROOT, "Allocation rate: %03dMiB/s", bytesToMebibytes(this.allocationRateCalculator.bytesAllocatedPerSecond(used))),
|
| 36 | String.format(Locale.ROOT, "Allocated: %2d%% %03dMiB", total * 100L / max, bytesToMebibytes(total))
|
| 37 | )
|
| 38 | );
|
| 39 | }
|
| 40 |
|
| 41 | private static long bytesToMebibytes(final long used) {
|
| 42 | return used / 1024L / 1024L;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public boolean isAllowed(final boolean reducedDebugInfo) {
|
| 47 | return true;
|
| 48 | }
|
| 49 |
|
| 50 | @Environment(EnvType.CLIENT)
|
| 51 | private static class AllocationRateCalculator {
|
| 52 | private static final int UPDATE_INTERVAL_MS = 500;
|
| 53 | private static final List<GarbageCollectorMXBean> GC_MBEANS = ManagementFactory.getGarbageCollectorMXBeans();
|
| 54 | private long lastTime = 0L;
|
| 55 | private long lastHeapUsage = -1L;
|
| 56 | private long lastGcCounts = -1L;
|
| 57 | private long lastRate = 0L;
|
| 58 |
|
| 59 | private long bytesAllocatedPerSecond(final long currentHeapUsage) {
|
| 60 | long time = System.currentTimeMillis();
|
| 61 | if (time - this.lastTime < 500L) {
|
| 62 | return this.lastRate;
|
| 63 | }
|
| 64 |
|
| 65 | long gcCounts = gcCounts();
|
| 66 | if (this.lastTime != 0L && gcCounts == this.lastGcCounts) {
|
| 67 | double multiplier = (double)TimeUnit.SECONDS.toMillis(1L) / (time - this.lastTime);
|
| 68 | long delta = currentHeapUsage - this.lastHeapUsage;
|
| 69 | this.lastRate = Math.round(delta * multiplier);
|
| 70 | }
|
| 71 |
|
| 72 | this.lastTime = time;
|
| 73 | this.lastHeapUsage = currentHeapUsage;
|
| 74 | this.lastGcCounts = gcCounts;
|
| 75 | return this.lastRate;
|
| 76 | }
|
| 77 |
|
| 78 | private static long gcCounts() {
|
| 79 | long total = 0L;
|
| 80 |
|
| 81 | for (GarbageCollectorMXBean gcBean : GC_MBEANS) {
|
| 82 | total += gcBean.getCollectionCount();
|
| 83 | }
|
| 84 |
|
| 85 | return total;
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|