简体   繁体   English

Java Character vs char:内存使用情况如何?

[英]Java Character vs char: what about memory usage?

In one of my classes I have a field of type Character . 在我的一个课程中,我有一个Character字段。 I preferred it over char because sometimes the field has "no value" and null seams to me the cleanest way to represent this (lack of) information. 我更喜欢它了char ,因为有时领域没有“价值”和null接缝我代表本(缺乏)信息最彻底的方法。

However I'm wondering about the memory footprint of this approach. 但是我想知道这种方法的内存占用情况。 I'm dealing with hundred of thousands of objects and the negligible difference between the two options may now deserve some investigation. 我正在处理成千上万的物体,这两种选择之间可以忽略不计的差异现在值得进行一些调查。

My first bet is that a char takes two bytes whereas a Character is an object, and so it takes much more in order to support its life cycle. 我的第一个赌注是char占用两个字节,而Character是一个对象,所以它需要更多才能支持它的生命周期。 But I know boxed primitives like Integer , Character and so on are not ordinary classes (think about boxing and unboxing), so I wonder if the JVM can make some kind of optimization under the hood. 但我知道盒装原语如IntegerCharacter等不是普通的类(想想装箱和拆箱),所以我想知道JVM是否可以在引擎盖下进行某种优化。

Furthermore, are Character s garbage collected like the other stuff or have a different life cycle? 还有,像其他东西一样收集Character的垃圾还是有不同的生命周期? Are they pooled from a shared repository? 它们是从共享存储库汇集的吗? Is this standard or JVM implementation-dependent? 这个标准或JVM实现依赖吗?

I wasn't able to find any clear information on the Internet about this issue. 我无法在互联网上找到有关此问题的任何明确信息。 Can you point me to some information? 你能指点我一些信息吗?

If you are you using Character to create character then prefer to use 如果您使用Character创建角色,则更喜欢使用

Character.valueOf('c'); // it returns cached value for 'c' 

Character c = new Character('c');// prefer to avoid

Following is an excerpt from javadoc. 以下是javadoc的摘录。

If a new Character instance is not required, this method Character.valueOf() should generally be used in preference to the constructor Character(char) , as this method is likely to yield significantly better space and time performance by caching frequently requested values. 如果不需要新的Character实例,则通常应优先使用此方法Character.valueOf() ,而不是构造函数Character(char) ,因为此方法可能通过缓存频繁请求的值来显着提高空间和时间性能。

As you stated, a Character object can be null , so it has to take more place in RAM than a regular char which cannot be null : in a way, Character s are a superset of char s. 正如你所说,一个Character对象可以为null ,因此它必须在RAM中占用比不能为null的常规char更多的位置:在某种程度上, Character s是char的超集。

However, in a given part of your code, the JIT compiler might be able to detect that you Character is never null an is always used as a regular char and optimize that part so that your Character uses no more RAM or execution is no slower. 但是,在代码的给定部分中,JIT编译器可能能够检测到您的Character永远不会为null并始终用作常规char并优化该部分,以便您的Character不再使用RAM或执行速度不会慢。 I'm just speculating on this very point, though, I don't know if a JIT can actually perform this precise optimization. 我只是在猜测这一点,但我不知道JIT是否可以实际执行这种精确的优化。

Use int instead. 请改用int Use -1 to represent "no char". 使用-1表示“无字符”。

Lots of precedence of this pattern, for example int read() in java.io.Reader 此模式的优先级很多,例如int read() in java.io.Reader

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

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