General formula for calculating memory usage

来源:互联网 发布:淘宝助理旧版本下载 编辑:程序博客网 时间:2024/05/21 17:22
 

General formula for calculating memory usage

In general, the heap memory used by a Java object in Hotspot consists of:

  • an object header, consisting of a few bytes of "housekeeping" information;
  • memory for primitive fields, according to their size (see below);
  • memory for reference fields (4 bytes each);
  • padding: potentially a few "wasted" unused bytes after the object data, to make every object start at an address that is a convenient multiple of bytes and reduce the number of bits required to represent a pointer to an object.

Sizes of primitive types

In case you're not familiar with the byte size of the different Java primitive data types, here is the complete list:

Java typeBytes requiredboolean1bytechar2shortint4floatlong8double