MemoryMetrics
数据结构
| 字节索引 | 类型 | 字段含义 |
|---|---|---|
| 0-3 | float32 | 物理内存使用率 [%] |
| 4-5 | uint16 | 已用物理内存 [MiB] |
| 6-7 | uint16 | 实际可用物理内存 [MiB],包含可回收缓存/Buffer |
| 8-9 | uint16 | 已用交换空间 Swap [MiB] |
| 10-11 | uint16 | 已用共享内存 tmpfs/shm [MiB] |
| 12-15 | uint32 | 累计硬缺页中断次数 |
| 16 | uint8 | 是否触发过 OOM Killer,0 为否,1 为是 |
| 17-19 | uint8 | 保留字段,必须为 0 |
MemoryMetrics 固定为 20 字节
示例
C++ 示例
struct MemoryMetrics {
float usage;
uint16_t used;
uint16_t available;
uint16_t swap_used;
uint16_t shm_used;
uint32_t major_page_faults;
uint8_t has_oom_event;
uint8_t reserved[3];
};
Rust 示例
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct MemoryMetrics {
pub usage: f32,
pub used: u16,
pub available: u16,
pub swap_used: u16,
pub shm_used: u16,
pub major_page_faults: u32,
pub has_oom_event: u8,
pub reserved: [u8; 3],
}
const _: () = {
assert!(core::mem::size_of::<MemoryMetrics>() == 20);
assert!(core::mem::align_of::<MemoryMetrics>() == 4);
};
Dart 示例
import 'dart:typed_data';
final class MemoryMetricsView {
static const int byteLength = 20;
final ByteData data;
MemoryMetricsView(ByteBuffer buffer, [int offsetInBytes = 0])
: data = ByteData.view(buffer, offsetInBytes, byteLength);
double get usage => data.getFloat32(0, Endian.little);
int get used => data.getUint16(4, Endian.little);
int get available => data.getUint16(6, Endian.little);
int get swapUsed => data.getUint16(8, Endian.little);
int get shmUsed => data.getUint16(10, Endian.little);
int get majorPageFaults => data.getUint32(12, Endian.little);
bool get hasOomEvent => data.getUint8(16) != 0;
}
Java 示例
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public final class MemoryMetricsView {
public static final int BYTE_LENGTH = 20;
private final ByteBuffer buffer;
private final int offset;
public MemoryMetricsView(ByteBuffer buffer, int offset) {
if (offset < 0 || offset + BYTE_LENGTH > buffer.capacity()) {
throw new IndexOutOfBoundsException("MemoryMetrics payload requires 20 bytes");
}
this.buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);
this.offset = offset;
}
public float usage() { return buffer.getFloat(offset); }
public int used() { return Short.toUnsignedInt(buffer.getShort(offset + 4)); }
public int available() { return Short.toUnsignedInt(buffer.getShort(offset + 6)); }
public int swapUsed() { return Short.toUnsignedInt(buffer.getShort(offset + 8)); }
public int shmUsed() { return Short.toUnsignedInt(buffer.getShort(offset + 10)); }
public long majorPageFaults() { return Integer.toUnsignedLong(buffer.getInt(offset + 12)); }
public boolean hasOomEvent() { return buffer.get(offset + 16) != 0; }
}