DebugEntryMemory.java

net.minecraft.client.gui.components.debug.DebugEntryMemory

信息

  • 全限定名:net.minecraft.client.gui.components.debug.DebugEntryMemory
  • 类型:public class
  • 包:net.minecraft.client.gui.components.debug
  • 源码路径:src/main/java/net/minecraft/client/gui/components/debug/DebugEntryMemory.java
  • 起始行号:L16
  • 实现:DebugScreenEntry
  • 职责:

    TODO

字段/常量

  • GROUP

    • 类型: Identifier
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

  • allocationRateCalculator

    • 类型: DebugEntryMemory.AllocationRateCalculator
    • 修饰符: private final
    • 源码定位: L18
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.components.debug.DebugEntryMemory.AllocationRateCalculator
    • 类型: class
    • 修饰符: private static
    • 源码定位: L46
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public void display(DebugScreenDisplayer displayer, Level serverOrClientLevel, LevelChunk clientChunk, LevelChunk serverChunk) @ L20

  • 方法名:display
  • 源码定位:L20
  • 返回类型:void
  • 修饰符:public

参数:

  • displayer: DebugScreenDisplayer
  • serverOrClientLevel: Level
  • clientChunk: LevelChunk
  • serverChunk: LevelChunk

说明:

TODO

private static long bytesToMebibytes(long used) @ L36

  • 方法名:bytesToMebibytes
  • 源码定位:L36
  • 返回类型:long
  • 修饰符:private static

参数:

  • used: long

说明:

TODO

public boolean isAllowed(boolean reducedDebugInfo) @ L40

  • 方法名:isAllowed
  • 源码定位:L40
  • 返回类型:boolean
  • 修饰符:public

参数:

  • reducedDebugInfo: boolean

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class DebugEntryMemory implements DebugScreenEntry {
    private static final Identifier GROUP = Identifier.withDefaultNamespace("memory");
    private final DebugEntryMemory.AllocationRateCalculator allocationRateCalculator = new DebugEntryMemory.AllocationRateCalculator();
 
    @Override
    public void display(DebugScreenDisplayer displayer, @Nullable Level serverOrClientLevel, @Nullable LevelChunk clientChunk, @Nullable LevelChunk serverChunk) {
        long max = Runtime.getRuntime().maxMemory();
        long total = Runtime.getRuntime().totalMemory();
        long free = Runtime.getRuntime().freeMemory();
        long used = total - free;
        displayer.addToGroup(
            GROUP,
            List.of(
                String.format(Locale.ROOT, "Mem: %2d %03dMiB", total * 100L / max, bytesToMebibytes(total))
            )
        );
    }
 
    private static long bytesToMebibytes(long used) {
        return used / 1024L / 1024L;
    }
 
    @Override
    public boolean isAllowed(boolean reducedDebugInfo) {
        return true;
    }
 
    @OnlyIn(Dist.CLIENT)
    private static class AllocationRateCalculator {
        private static final int UPDATE_INTERVAL_MS = 500;
        private static final List<GarbageCollectorMXBean> GC_MBEANS = ManagementFactory.getGarbageCollectorMXBeans();
        private long lastTime = 0L;
        private long lastHeapUsage = -1L;
        private long lastGcCounts = -1L;
        private long lastRate = 0L;
 
        private long bytesAllocatedPerSecond(long currentHeapUsage) {
            long time = System.currentTimeMillis();
            if (time - this.lastTime < 500L) {
                return this.lastRate;
            } else {
                long gcCounts = gcCounts();
                if (this.lastTime != 0L && gcCounts == this.lastGcCounts) {
                    double multiplier = (double)TimeUnit.SECONDS.toMillis(1L) / (time - this.lastTime);
                    long delta = currentHeapUsage - this.lastHeapUsage;
                    this.lastRate = Math.round(delta * multiplier);
                }
 
                this.lastTime = time;
                this.lastHeapUsage = currentHeapUsage;
                this.lastGcCounts = gcCounts;
                return this.lastRate;
            }
        }
 
        private static long gcCounts() {
            long total = 0L;
 
            for (GarbageCollectorMXBean gcBean : GC_MBEANS) {
                total += gcBean.getCollectionCount();
            }
 
            return total;
        }
    }
}

引用的其他类