Java虚拟机如何分配内存?
我想知道Java VM如何分配内存? JAVA在我们计算机的物理内存中分配内存吗? 现在我正在尝试比较两种数据结构,链表和数组,注意页面错误和缓存命中/未命中,以查看每个数据结构在真正低级别(硬盘,内存,缓存级别)的性能
I am wondering how does Java VM allocate memory?
does JAVA allocate memory in the physical memory in our computer?
Right now I am trying to compare two data structures, linked list and array taking caution in terms of page fault, and cache hit/miss, to see the performance of each data structure in really low level(harddisk, memory, cache level)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否回答了您的问题,但是 IBM 有一篇关于JVM 如何在 Windows / Linux 上使用本机内存。
I don't know if this answers your question but IBM has a good article about how JVM uses native memory on Windows / Linux.
Java 在启动时从操作系统分配连续内存。一旦它拥有堆所需的内存,它就会控制内存的分配方式。
Java 中的内存是连续分配的,因此每个对象很可能会按照分配的顺序出现在内存中。 (但这并不能保证)
此外,如果对象在次要集合中幸存下来,则很可能会按照对象发现的顺序进行复制。 (同样不能保证)
就我个人而言,如果您希望随机访问,我会使用 ArrayList;如果您想插入列表末尾以外的任何位置,我会使用 LinkedList。
如果将任何内容交换到磁盘,Java 的性能就会非常糟糕。我建议确保它足够小以始终适合内存。它的缓存行为不容易控制,您能做的最好的事情就是尝试 JVM 如何优化您的代码及其缓存行为。
Java allocates continous memory from the OS on startup. Once it has the memory it needs for the heap, it controls how memory is allocated.
Memory in Java is allocated continously so it highly likely that each object will apear in memory in the order it is allocated. (But this is not guaranteed)
Additionally if the object survives a minor collection, it is likely to be copied in the order the objects are discovered. (again not guaranteed)
Personally I would use an ArrayList if you expect random access, and LinkedList if you want to insert anywhere but the end of the List.
Java performs very badly is any of it is swapped to disk. I would suggest ensuring it is small enough to fit into memory at all times. Its cache behaviour is not easily controlled and the best thing you can do is experiment with how the JVM optimises your code and its caching behaviour.