Java内存使用 - 原语 [英] Java Memory usage - primitives

查看:124
本文介绍了Java内存使用 - 原语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

算法第4版中引用以下内容



例如,如果您的计算机上有1GB内存(10亿字节),则一次不能在内存中容纳超过3200万个int值或1600万个double值。



int - 4个字节



32百万x 4 = 1.28亿字节



帮助我理解,为什么我们不能适应3200万个int值,上面的1.28亿个字节大约是1GB或10亿个字节的总内存消耗的1/10。

解决方案

这取决于您如何组织数据。在Java中,每个对象都有开销,这与JVM有关,但通常为8或16个字节。因此,如果将每个int值包装到一个对象中(例如 Integer ),则可能超过1GB。但是,如果你将它分配为 int [] 数组,那么它应该很容易适合1GB。



而且,这与问题没有严格关系,但反映给@ Anony-Mousse的评论,有微控制器的JVM ,我很确定这些JVM中的对象大小低于8个字节(尽管我没有找到确切的数据)。 / p>

Quoting the following from Algorithms 4th edition

"For example, if you have 1GB of memory on your computer (1 billion bytes), you cannot fit more than about 32 million int values or 16 million double values in memory at one time."

int - 4 bytes

32 million x 4 = 128 million bytes

Help me understand, why we can't fit 32 million int values, the above 128 million bytes is about 1/10 of the overall memory consumption of 1GB or 1 billion bytes.

解决方案

That depends on how you organize your data. In Java, every object has an overhead, this is JVM dependent, but typically 8 or 16 bytes. So if you wrap every int value into an object (like Integer), then you may exceed 1GB. But, if you allocate it as an int[] array, then it should fit into 1GB easily.

And, this is not strictly related to the question, but reflecting to @Anony-Mousse's comment, there are JVMs for microcontrollers, and I am pretty sure that object size is below 8 bytes in those JVMs (though I didn't find exact data).

这篇关于Java内存使用 - 原语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆