Java 在 IntelliJ IDEA 中正确设置堆大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18631038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting heap size in IntelliJ IDEA correctly
提问by Lukas Arvidsson
I have this peculiar problem with running a Processing application in IntelliJ IDEA. I want to save a large image and to do this I run in to the following exception:
我在 IntelliJ IDEA 中运行处理应用程序时遇到了这个特殊的问题。我想保存一个大图像,为此我遇到了以下异常:
Exception in thread "Animation Thread" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.awt.image.BufferedImage.(BufferedImage.java:331) at processing.core.PImage.saveImageIO(PImage.java:3117) at processing.core.PImage.save(PImage.java:3297) at OffScreenRender.stopRender(OffScreenRender.java:38) at MainVecField.draw(MainVecField.java:93) at processing.core.PApplet.handleDraw(PApplet.java:2305) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243) at processing.core.PApplet.run(PApplet.java:2176) at java.lang.Thread.run(Thread.java:724)
线程“动画线程”中的异常 java.lang.OutOfMemoryError: java.awt.image.DataBufferInt.(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:467) 处的 Java 堆空间java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.awt.image.BufferedImage.(BufferedImage.java:331) at processing.core.PImage.saveImageIO(PImage.java:3117) at processing。 core.PImage.save(PImage.java:3297) at OffScreenRender.stopRender(OffScreenRender.java:38) at MainVecField.draw(MainVecField.java:93) at processing.core.PApplet.handleDraw(PApplet.java:2305) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243) at processing.core.PApplet.run(PApplet.java:2176) at java.lang.Thread.run(Thread.java:724)
So clearly there is some memory allocation problem here. The problem is that when i change the .vmoptions files in IntelliJ I get the same results, they have no effect. The "memory" number at the lower right corner of the IDE increases accordingly but it does not seem to let my application allocate more memory.
很明显,这里存在一些内存分配问题。问题是,当我在 IntelliJ 中更改 .vmoptions 文件时,我得到了相同的结果,但它们没有任何效果。IDE 右下角的“内存”数字相应增加,但它似乎并没有让我的应用程序分配更多内存。
When I run the processing application in the processing IDE I can manage to save much larger files by setting a large heap size.
当我在处理 IDE 中运行处理应用程序时,我可以通过设置大堆大小来设法保存更大的文件。
In IntelliJ nothing over 256mb seems to make a difference. So my question is how do I set a larger heap size in IntelliJ that allows my application to allocate more memory?
在 IntelliJ 中,超过 256mb 的大小似乎没有什么不同。所以我的问题是如何在 IntelliJ 中设置更大的堆大小以允许我的应用程序分配更多内存?
Thank you for your help!
感谢您的帮助!
I attach the code for my project in case anyone wants to test it out:
我附上我的项目的代码,以防有人想测试它:
import processing.core.*;
public class TestClassMain extends PApplet
{
public static void main(String args[])
{
PApplet.main(new String[] { "--present", "TestClassMain" });
}
//Global variables
OffScreenRender screenRender;
int c = 0;
//Variables for offScreenRender
int resFactor = 10;
boolean offScreenRenderSwitch = false;
public void setup()
{
size(1000,700);
background(0);
stroke(255);
//Initialize the render object
screenRender = new OffScreenRender(this, 11000, 7950, resFactor);
}
public void draw()
{
frameRate(30);
stroke(255);
//Check if the render should be saved
if(offScreenRenderSwitch == true){screenRender.startRender();}
background(0,0,0);
rect(20+c,height/2,60,60);
if(offScreenRenderSwitch == true)
{
screenRender.stopRender();
offScreenRenderSwitch = false;
}
c += 2;
}
public void keyPressed()
{
//Render the graphics to an offScreen buffer
offScreenRenderSwitch = true;
}
}
import processing.core.*;
/**
* Renders the processingsketch to a .tif file at specified resolution.
*/
public class OffScreenRender
{
PApplet parent; // The parent PApplet
PGraphics render;
int width;
int height;
int resFactor;
OffScreenRender(PApplet p, int width_, int height_, int resFactor_)
{
//Initialize variables
parent = p;
width = width_;
height = height_;
resFactor = resFactor_;
render = parent.createGraphics(width, height);
}
//Render the image
void startRender()
{
parent.beginRecord(render);
render.scale(resFactor);
PApplet.println("first step");
}
//Render the image
void stopRender()
{
parent.endRecord();
PApplet.println("second step");
render.save("hej");
PApplet.println("final step");
}
}
采纳答案by Vic
Changing vmoptions file adjusts the memory used by IntelliJ, but what you're having here is a shortage of memory of JRE that is launched by IntelliJ to execute your application. You need to adjust the memory setting in VM options part of the Run/Debug configuration, for example:
更改 vmoptions 文件会调整 IntelliJ 使用的内存,但您在这里遇到的是 IntelliJ 为执行您的应用程序而启动的 JRE 内存不足。您需要在 Run/Debug 配置的 VM 选项部分调整内存设置,例如:
回答by Peter Lawrey
IntelliJ shows you the amount used and the current size of the heap. You are trying to set the maximum which it does not show.
IntelliJ 显示已使用的数量和堆的当前大小。您正在尝试设置未显示的最大值。
You can attach VisualVM to IntellIJ to see the maximum by running jvisualvm
您可以通过运行将 VisualVM 附加到 IntellIJ 以查看最大值 jvisualvm