Java 在一个不断计算更高值并将其存储在磁盘上的系统中,存储需求何时会超过RAM?
我正在做一个可以产生无穷多数字的项目Java 在一个不断计算更高值并将其存储在磁盘上的系统中,存储需求何时会超过RAM?,java,multithreading,performance,math,recursion,Java,Multithreading,Performance,Math,Recursion,我正在做一个可以产生无穷多数字的项目 :我已经成功地解决了与之相关的线程问题。此外,我决定将sequencer的结果存储在硬盘上,这样我就可以计算更多的数字(并确保它们都收敛到1),每次存储结果的HashMap达到400000(在我的计算机上是一个合理的大小,大约1GB/8GB)键/值对时,我都会这样做,然后加载下一个400000。假设值是FinalSequencerReports,因为值越来越大,趋于∞存储的值最终应该超过我的RAM容量,有没有办法绕过这个限制(或者甚至在存储400000实例的
:我已经成功地解决了与之相关的线程问题。此外,我决定将sequencer的结果存储在硬盘上,这样我就可以计算更多的数字(并确保它们都收敛到1),每次存储结果的HashMap
达到400000
(在我的计算机上是一个合理的大小,大约1GB/8GB)键/值对时,我都会这样做,然后加载下一个400000
。假设值是FinalSequencerReport
s,因为值越来越大,趋于∞代码>存储的值最终应该超过我的RAM容量,有没有办法绕过这个限制(或者甚至在存储400000
实例的数量超过我的RAM容量的情况下,我会遇到什么样的情况?或者任何RAM容量的公式?)注意:在FinalSequencerReport
类中有一个字符串
,它将始终为空
类存储在HashMap
中:
公共静态最终类FinalSequencerReport扩展SequencerReport实现{
publicstaticcomparator您需要定期与a配对-我建议检查RAM的频率要比检查HDD的频率高很多
如果您想估计空间使用情况,您可以,但要注意:由于对象的内部表示可能随时发生变化,您可能会得到不正确的结果,这是特定于实现的,对象大小无法在java中准确测量,AFAIK
还请注意,可用RAM的上限始终取决于您的JVM设置,Oracle JVM设置可以通过-Xmx
进行更改,可能的后缀包括m
和g
,例如:-Xmx2g
-不要摆弄-Xms
,因为如果不产生奇怪的碰撞,它会严重降低速度这是因为您有效地规避了每一个JVM优化
可用文件系统空间和可用RAM:
/* Total number of processors or cores available to the JVM */
System.out.println("Available processors (cores): " +
Runtime.getRuntime().availableProcessors());
/* Total amount of free memory available to the JVM */
System.out.println("Free memory (bytes): " +
Runtime.getRuntime().freeMemory());
/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (bytes): " +
(maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
/* Total memory currently in use by the JVM */
System.out.println("Total memory (bytes): " +
Runtime.getRuntime().totalMemory());
/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();
/* For each filesystem root, print some info */
for (File root : roots) {
System.out.println("File system root: " + root.getAbsolutePath());
System.out.println("Total space (bytes): " + root.getTotalSpace());
System.out.println("Free space (bytes): " + root.getFreeSpace());
System.out.println("Usable space (bytes): " + root.getUsableSpace());
}
对象大小近似值:
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}
如果按顺序计算所有结果,则没有理由使用biginger
,因为在合理的时间内保持long
范围不变。请注意,biginger
会大大降低计算速度。
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}