What is the storage cost for a boxed primitive in Java?

前端 未结 4 1844
醉酒成梦
醉酒成梦 2020-12-15 21:04

How large, in bytes, is a boxed primitive like java.lang.Integer or java.lang.Character in Java?

An int is 4 bytes, a typical

4条回答
  •  一生所求
    2020-12-15 21:27

    It's more than that.

    Every object reference has additional overhead, such as a Class reference. Not only that, your 4-byte pointer isn't quite accurate. It's a reference, so it's an ID plus a pointer, AND that pointer may be 8 bytes if you are on a 64 bit JVM.

    There also appear to be VM implementation differences. Best way to be sure on this would be to pull it up in a profiler.

    My (Super SWAG) estimate would be. Object reference 16 bytes (64 bit JVM) Class reference 16 bytes primitive value 4 bytes (Assuming int.) Total. 36 bytes.

    EDIT: Now that your specify 32-bit JVM my SWAG would be 20 bytes using same math above.

提交回复
热议问题