简体   繁体   English

Java中具有空值的char数组的内存使用情况

[英]Memory usage of a char array with null values in Java

I'm attempting to calculate the memory usage of a trie search tree implementation in java.我正在尝试计算 java 中 trie 搜索树实现的内存使用情况。 Each node contains a char array of size 52, with many of the indices being null values.每个节点都包含一个大小为 52 的 char 数组,其中许多索引为空值。 I'm aware of the memory consumption of an array as well as each char in the array, but I'm unsure of the memory consumption of the null values.我知道数组的内存消耗以及数组中的每个字符,但我不确定空值的内存消耗。 As far as I understand, JVM reserves space for the char and the reference to it, even while being null.据我了解,JVM 为 char 和对它的引用保留空间,即使是 null 也是如此。 Is this correctly understood?这理解正确吗? How many bytes would a null value in the array then take up, the same as if a char was available on that index?然后数组中的空值将占用多少字节,就像该索引上可用的 char 一样?

Turning the array into an arrayList is not possible, and I don't seek advice on optimizing the memory usage, only advice on calculating the memory usage of said null values.将数组转换为 arrayList 是不可能的,我不寻求优化内存使用的建议,只寻求计算所述空值的内存使用的建议。

I'll happily answer any and all questions if needed!如果需要,我会很乐意回答任何问题!

The size of a char is always 16 bits, see https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Character.html#SIZE . char的大小始终为 16 位,请参阅https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Character.html#SIZE The length of array times the element size plus the (Java Virtual Machine implementation dependent) size of the array's object header equals the amount of used memory.数组的长度乘以元素大小加上数组对象头的(Java 虚拟机实现相关)大小等于已用内存量。

As @Mark Rotteveel stated null is not a valid char .正如@Mark Rotteveel 所说, null不是有效的char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM