Ultimately you always have a finite max of heap to use no matter what platform you are running on.
(最终,无论您在什么平台上运行,都始终可以使用有限的最大堆。)
In Windows 32 bit this is around 2GB
(not specifically heap but total amount of memory per process). (在Windows 32位中,这大约为2GB
(不是专门用于堆,而是每个进程的内存总量)。)
It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing). (碰巧的是Java选择减小默认值(大概是为了让程序员在没有遇到这个问题且不必仔细检查它们在做什么的情况下,无法创建内存分配失控的程序)。)
So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using.
(因此,考虑到这种情况,您可以采用多种方法来确定所需的内存量或减少正在使用的内存量。)
One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. (Java或C#等垃圾收集语言的一个常见错误是,保留对不再使用的对象的引用,或者在可以重用它们时分配许多对象。)
As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them. (只要对象引用了它们,它们将继续使用堆空间,因为垃圾收集器不会删除它们。)
In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place.
(在这种情况下,您可以使用Java内存探查器来确定程序中哪些方法正在分配大量对象,然后确定是否有办法确保不再引用它们,或者首先不分配它们。)
One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/ . (我过去使用的一个选项是“ JMP” http://www.khelekore.org/jmp/ 。)
If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program.
(如果确定您出于某种原因分配这些对象,并且需要保留引用(取决于实际情况),则仅需要在启动程序时增加最大堆大小。)
However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need. (但是,一旦执行了内存分析并了解了对象的分配方式,就应该对所需的内存有一个更好的了解。)
In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem.
(通常,如果您不能保证程序会在一定数量的内存中运行(也许取决于输入大小),则总是会遇到此问题。)
Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. (仅在用尽所有这些之后,您才需要研究将对象缓存到磁盘等中。在这一点上,您应该有一个很好的理由说“我需要Xgb的内存”,您不能通过改进来解决它。您的算法或内存分配模式。)
Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful. (通常,只有在大型数据集(例如数据库或某些科学分析程序)上运行的算法才会出现这种情况,然后诸如缓存和内存映射IO之类的技术就变得有用。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…