Java JVM 根据操作系统保持内存分配
我在 Windows 7 64 位上运行 1.6.0_25 64 位。
我试图让 JVM 以最大内存运行,因为我的应用程序内存非常密集...但遗憾的是...内存分配没有建立起来,并且 Windows 中存在大量页面错误,无法继续带来下一组虚拟内存中。
我正在运行 java -Xms2G -Xmx3G 测试
以下代码是我尝试将整个文件引入...这样我就不会在读入时遇到页面错误。
File f = new File("veryLARGEfile.txt");
FileInputStream in = new FileInputStream(f);
int i = (int) f.length();
System.out.println("Bytes in file: " + i);
byte[] file = new byte[i];
i = in.read(file);
System.out.println("Bytes read: " + i);
执行此方法我可以在 Windows 中看到任务管理器在读取文件时系统内存达到2G...但是一旦读取完成...内存就会再次下降!!!!
这是一个主要问题......我需要整个字节数组保留在活动内存中。
谢谢你, 我
已经修改了代码以使用基本数组类型 int[][] 和 float[][] 来保存我的数据,而不是保留包含 int 和 float 的对象的 ArrayList。
这样做,我发现我的java内存没有被交换(所以,我猜堆内存的处理方式与这里的堆栈有点不同)[哦,我也将所有代码更改为静态类型 - 我知道,非常糟糕编程风格]
我现在遇到的问题是如何处理我的 HashMap...我尝试构建查找表的所有尝试都失败了,构建运行时间为 O(n^2)!
I am running 1.6.0_25 64bit on windows 7 64bit.
I am trying to have the JVM run with maximum memory because my application is very memory intensive... but sadly... the memory allocation does not build up and there is a lot of Page Faults from windows to keep bringing the next set of virtual memory in.
I am running java -Xms2G -Xmx3G Test
The following code is my attempt at bringing a whole file in... such that i do not run into page faults for reading in.
File f = new File("veryLARGEfile.txt");
FileInputStream in = new FileInputStream(f);
int i = (int) f.length();
System.out.println("Bytes in file: " + i);
byte[] file = new byte[i];
i = in.read(file);
System.out.println("Bytes read: " + i);
doing this method i can see that in windows task manager the system reaches 2G worth of memory while it is reading the file... but once it is done reading it... the memory falls back down again!!!!
This is a major problem... i need the whole byte array to stay in active memory.
Thank you,
ey
I have modified the code to use basic array types int[][] and float[][] to hold my data instead of keeping ArrayLists of an object containing int's and float's.
doing this, i find that my java memory does not get swapped (so, i guess heap memory is treated a bit differently from stack here) [oh, i did change all the code to be static typed as well - i know, very bad programming style]
the issue that i am running into now is how to handle my HashMap... all my attempts of trying to build a lookup table is failing with O(n^2) running time to build!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进程内存的哪些部分保留在 RAM 中以及哪些部分换出是由操作系统决定的,而不是由 JVM 决定的。一般来说,操作系统会将经常访问的内存保留在 RAM 中 - 如果不是,为什么需要它呢?
可能有操作系统 API 允许您将某块内存“固定”在 RAM 中,但 Java 并未公开此功能。
但根据您的要求,您应该考虑使用文件 内存映射而不是显式读取它。它可能会快得多。
The decision what parts of a process's memory to keep in RAM and which to swap out is made by the OS, not by the JVM. Generally, the OS will keep memory that is accessed frequently in RAM - and if it isn't, why would you need it there?
There might be OS APIs that allow you to "pin" a certain piece of memory in RAM, but this functionality is not exposed by Java.
But for your requirements, you should look into having the file memory-mapped rather than reading it in explicitly. It will likely be much faster.
Java 相当努力地将其所有应用程序保留在内存中,或者换句话说,它会将交换作为最后的手段,因为如果将其任何内存交换到磁盘,它的性能就会非常糟糕。
如果您的 Java 应用程序正在交换到磁盘,那是因为您的应用程序使用了过多的系统内存。如果您的应用程序是内存密集型的并且您必须使用这么多内存,我建议确保您的系统有足够的内存。顺便提一句。您可以花约 500 美元购买一台 8 GB 的服务器,花 1000 美元购买新的 16 GB 工作站。 ;)
Java tries fairly hard to keep all its application in memory, or put another way, it swaps out as a last resort as it perform very badly if any of its memory is swapped to disk.
If your Java application is swapping to disk, its because your application uses too much memory for the system you have. If your application is memory intensive and you have to use this much memory I suggest sure your system has enough memory. BTW. You can buy a server with 8 GB for ~$500, and new 16 GB workstation for $1000. ;)