一、单位换算公式
1Bytes=8bits(1字节等于8位)
1KB=1024Bytes(1KB等于1024字节)
1MB=1024KB
1GB=1024MB
1TB=1024GB
在Java中一共有8种基本数据类型,其中有4种整型,2种浮点类型,1种用于表示Unicode编码的字符单元的字符类型和1种用于表示真值的boolean类型。(一个字节等于8个bit)
1.整型
类型 存储需求 bit数 取值范围 备注
int 4字节 4*8
short 2字节 2*8 -32768~32767
long 8字节 8*8
byte 1字节 1*8 -128~127
2.浮点型
类型 存储需求 bit数 取值范围 备注
float 4字节 4*8 float类型的数值有一个后缀F(例如:3.14F)
double 8字节 8*8 没有后缀F的浮点数值(如3.14)默认为double类型
3.char类型
类型 存储需求 bit数 取值范围 备注
char 2字节 2*8
4.boolean类型
类型 存储需求 bit数 取值范围 备注
boolean 1字节 1*8 false、true
二、常见对象占用内存
在java中空对象占八个字节,对象的引用占四个字节。所以上面那条语句所占的空间是4byte+8byte=12byte.java中的内存是以8的倍数来分配的,所以分配的内存是16byte.
Class O{
int i;
byte j;
String s;
}
View Code
其所占内存的大小是空对象(8)+int(4)+byte(1)+String引用(4)=17byte,因要是8的整数倍,所以其占大小为24byte.
public class MemoryTest {
public static void main(String[] args) {
List<Long> unPay=new ArrayList<>();
unPay.add(12L);
unPay.add(13L);
unPay.add(14L);
List<Long> unFunds=new ArrayList<>();
unFunds.add(15L);
unFunds.add(16L);
unFunds.add(17L);
List<Long> maintainFunds=new ArrayList<>();
maintainFunds.add(18L);
maintainFunds.add(19L);
maintainFunds.add(20L);
List<Long> maintainPay=new ArrayList<>();
maintainFunds.add(21L);
maintainFunds.add(22L);
maintainFunds.add(23L);
Data1 data1=new Data1();//8
data1.setBankName("建设银行");//3*4
data1.setBankId(99L);//8
data1.setBankCode("CCBC");//4
data1.setBankCardType("debit");//5
data1.setDisplayFlag(true);//1
data1.setDisplayName("中国建设银行");//3*6
data1.setIconUrl("http://abc/ccbc");//15
data1.setPcIconUrl("http://abc/ccbc");//15
data1.setUnSupportPayPhysicalChannelId(unPay);//8*10
data1.setUnSupportFundsPhysicalChannelId(unFunds);//8*10
data1.setMaintainPayPhysicalChannelId(maintainPay);//8*10
data1.setMaintainFundsPhysicalChannelId(maintainFunds);//8*10
//占用空间398个byte===>400byte==>0.39KB
//10000个对象==>3900KB==>3MB
//100000个对象==>30MB
//1000000个对象==>300MB
}
}
/**
* 该对象的数据占用空间 8+3*4+8+3*5+3*5+1+3*10+
*
* 空对象占用:8个字节
*/
class Data1{
/**
* 1个字符3个byte
*/
private String bankName;
/**
* 8byte
*/
private Long bankId;
/**
* 1个字符3个byte
*/
private String bankCode;
/**
* 1个字符3个byte
*/
private String bankCardType;
/**
* 1byte
*/
private boolean displayFlag;
/**
* 1个字符3个byte
*/
private String displayName;
/**
* 1个字符3个byte
*/
private String iconUrl;
/**
* 1个字符3个byte
*/
private String pcIconUrl;
/**
* 8byte*10;
*/
private List<Long> unSupportPayPhysicalChannelId;
/**
* 8byte*10;
*/
private List<Long> unSupportFundsPhysicalChannelId;
/**
* 8byte*10;
*/
private List<Long> maintainPayPhysicalChannelId;
/**
* 8byte*10;
*/
private List<Long> maintainFundsPhysicalChannelId;
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public Long getBankId() {
return bankId;
}
public void setBankId(Long bankId) {
this.bankId = bankId;
}
public String getBankCode() {
return bankCode;
}
public void setBankCode(String bankCode) {
this.bankCode = bankCode;
}
public String getBankCardType() {
return bankCardType;
}
public void setBankCardType(String bankCardType) {
this.bankCardType = bankCardType;
}
public boolean isDisplayFlag() {
return displayFlag;
}
public void setDisplayFlag(boolean displayFlag) {
this.displayFlag = displayFlag;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
public String getPcIconUrl() {
return pcIconUrl;
}
public void setPcIconUrl(String pcIconUrl) {
this.pcIconUrl = pcIconUrl;
}
public List<Long> getUnSupportPayPhysicalChannelId() {
return unSupportPayPhysicalChannelId;
}
public void setUnSupportPayPhysicalChannelId(List<Long> unSupportPayPhysicalChannelId) {
this.unSupportPayPhysicalChannelId = unSupportPayPhysicalChannelId;
}
public List<Long> getUnSupportFundsPhysicalChannelId() {
return unSupportFundsPhysicalChannelId;
}
public void setUnSupportFundsPhysicalChannelId(List<Long> unSupportFundsPhysicalChannelId) {
this.unSupportFundsPhysicalChannelId = unSupportFundsPhysicalChannelId;
}
public List<Long> getMaintainPayPhysicalChannelId() {
return maintainPayPhysicalChannelId;
}
public void setMaintainPayPhysicalChannelId(List<Long> maintainPayPhysicalChannelId) {
this.maintainPayPhysicalChannelId = maintainPayPhysicalChannelId;
}
public List<Long> getMaintainFundsPhysicalChannelId() {
return maintainFundsPhysicalChannelId;
}
public void setMaintainFundsPhysicalChannelId(List<Long> maintainFundsPhysicalChannelId) {
this.maintainFundsPhysicalChannelId = maintainFundsPhysicalChannelId;
}
}
View Code
结论:
//上述对象占用空间398个byte===>400byte==>0.39KB
//10000个对象==>3900KB==>3MB
//100000个对象==>30MB
//1000000个对象==>300MB