在 IntelliJ IDEA 中正确设置堆大小
我在IntelliJ IDEA中运行处理应用程序时遇到了这个奇怪的问题。我想保存一个大图像,为此,我遇到了以下异常:
线程“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)
很明显,这里存在一些内存分配问题。问题是,当我在IntelliJ中更改.vmoptions文件时,我得到相同的结果,它们没有效果。IDE 右下角的“内存”数字相应地增加,但它似乎没有让我的应用程序分配更多内存。
当我在处理IDE中运行处理应用程序时,我可以通过设置较大的堆大小来设法保存更大的文件。
在IntelliJ中,超过256mb的任何内容似乎都没有区别。所以我的问题是如何在IntelliJ中设置更大的堆大小,以允许我的应用程序分配更多内存?
感谢您的帮助!
我附上我的项目的代码,以防有人想测试它:
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");
}
}