参考 https://stackoverflow.com/questions/383551/what-is-the-size-of-a-boolean-variable-in-java 和 https://stackoverflow.com/questions/1907318/why-is-javas-boolean-primitive-size-not-defined
取决于虚拟机的具体实现。
JAVA
class LotsOfBooleans {
boolean a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
boolean b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
boolean c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
boolean d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
boolean e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}
class LotsOfInts {
int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
int b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
int c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
int d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
int e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}
public class Test {
private static final int SIZE = 1000000;
public static void main(String[] args) throws Exception {
LotsOfBooleans[] first = new LotsOfBooleans[SIZE];
LotsOfInts[] second = new LotsOfInts[SIZE];
System.gc();
long startMem = getMemory();
for (int i=0; i < SIZE; i++) {
first[i] = new LotsOfBooleans();
}
System.gc();
long endMem = getMemory();
System.out.println ("Size for LotsOfBooleans: " + (endMem-startMem));
System.out.println ("Average size: " + ((endMem-startMem) / ((double)SIZE)));
System.gc();
startMem = getMemory();
for (int i=0; i < SIZE; i++) {
second[i] = new LotsOfInts();
}
System.gc();
endMem = getMemory();
System.out.println ("Size for LotsOfInts: " + (endMem-startMem));
System.out.println ("Average size: " + ((endMem-startMem) / ((double)SIZE)));
// Make sure nothing gets collected
long total = 0;
for (int i=0; i < SIZE; i++) {
total += (first[i].a0 ? 1 : 0) + second[i].a0;
}
System.out.println(total);
}
private static long getMemory() {
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
}
}重申一下,这是依赖于 VM 的,但是在我运行 AdoptOpenJDK build 17.0.7 的 Mac 笔记本电脑上,我得到了以下结果:
PLAINTEXT
Size for LotsOfBooleans: 95439088
Average size: 95.439088
Size for LotsOfInts: 336188816
Average size: 336.188816这表明 Sun 的 JVM 基本上可以将布尔值打包成一个字节。
推荐阅读
PVE 安装 (基于最新PVE8)及相关基础配置(换 Debian 源和 PVE 企业源)本文介绍 PVE 8 的安装及 Debian 源的更换和 PVE 源的更换
Fake Location 1.3.2.2 全功能解锁模块FakeLocation 1.3.2.2 全功能破解,来自52PoJie论坛
Fake Location 1.3.2.2专业版破解(虚拟定位软件)在 52Pojie 上看到的 Fake Location 的破解案例,虽然 APP 已经不能用了,但是破解参考价值还是很好的
OpenWRT 配置 AP 模式OpenWRT 配置 AP 模式
OpenWrt 进阶教程之无线中继配置指南OpenWrt进阶教程之无线中继配置指南
Android逆向 | 某麦网APP抢票接口加密参数分析及抢票工具编写Android逆向 | 某麦网APP抢票接口加密参数分析及抢票工具编写
OpenWRT 主路由模式和旁路由模式配置OpenWRT 主路由模式和旁路由模式配置
PVE 安装 OpenWRT (基于PVE 8)PVE 安装 OpenWRT(基于PVE 8.0
评论交流
暂无评论,欢迎分享您的见解
