[英]Use of Primitive `byte` increased space occupied when compared to Primitive `int`
I have a sample program for which I'm testing the memory occupied. 我有一个示例程序,正在测试其占用的内存。 I don't know where I'm going wrong or I'm implementing something wrong. 我不知道我要去哪里错或者我正在实施错误的东西。 But when I use byte
instead of int
, memory occupied by JVM is increased. 但是,当我使用byte
代替int
,JVM占用的内存增加了。 Below are two class files: 以下是两个类文件:
Class Test1: Class Test1:
public class Test1 {
public Test1 node1, node2;
DATATYPE a, b; // DATATYPE is Primitive `int` or Primitive `byte`
}
Class Test: 课堂测试:
public class Test {
public static void main(String args[]) throws IOException
{
System.out.println(getMemory());
Test1 root = new Test1();
Test1 currNode = root;
for(int i = 0; i < 10000000; i++)
{
currNode.node1 = new Test1();
currNode.a = 10;
currNode.b = 10;
currNode.node2 = currNode;
currNode = new Test1();
}
System.out.println(getMemory());
}
public static String getMemory()
{
HashMap<String, Double> hm = new HashMap<String, Double>();
Runtime runtime = Runtime.getRuntime();
hm.put("\nTotal Memory ", (double) runtime.totalMemory()/(1024*1024));
hm.put("\nFree Memory ", (double) runtime.freeMemory()/(1024*1024));
hm.put("\nUsedup Memory ", (double) (runtime.totalMemory() - runtime.freeMemory())/(1024*1024));
return hm.toString();
}
}
Output1 when DATATYPE = int: DATATYPE = int时的Output1:
{
Free Memory = 966.1398086547852,
Total Memory = 981.5,
Usedup Memory = 15.360191345214844}
{
Free Memory = 869.7226409912109,
Total Memory = 981.5,
Usedup Memory = 111.77735900878906
}
Output1 when DATATYPE = byte: DATATYPE =字节时的Output1:
{
Free Memory = 966.1398086547852,
Total Memory = 981.5,
Usedup Memory = 15.360191345214844
}
{
Free Memory = 765.0790710449219,
Total Memory = 981.5,
Usedup Memory = 216.42092895507812
}
I don't know if I'm doing something wrong. 我不知道我做错了什么。 What could be the reason for this? 这可能是什么原因?
Byte is a boxed primitive which requires more memory than primitives such as int, double, etc. Byte是一个装箱的原语,它比int,double等原语需要更多的内存。
Object references such as boxed primitives (eg Byte, Integer, Double) have additional overhead such as a Class reference. 诸如盒装原语(例如Byte,Integer,Double)之类的对象引用具有诸如Class引用之类的额外开销。
Therefore using byte instead of Byte should resolve your issue. 因此,使用字节代替字节应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.