Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting heap size in IntelliJ IDEA correctly

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:

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)

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.

When I run the processing application in the processing IDE I can manage to save much larger files by setting a large heap size.

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?

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");
    }
}
like image 549
Lukas Arvidsson Avatar asked Sep 05 '13 08:09

Lukas Arvidsson


People also ask

What should be the max heap size IntelliJ?

Common options Limits the maximum memory heap size that the JVM can allocate for running IntelliJ IDEA. The default value depends on the platform. If you are experiencing slowdowns, you may want to increase this value, for example, to set the value to 2048 megabytes, change this option to -Xmx2048m .

What should I set my heap size to?

For a 64-bit JVM, a Java Heap size of 3 GB or 4 GB per JVM process is usually my recommended starting point. Your business traffic will typically dictate your dynamic memory footprint.

How do I fix invalid max heap size?

64 bit JVM installed on Solaris machines runs with a 32-bit model if you don't specify either -d32 or -d64, which won't accept a Maximum heap size of 4GB, hence "invalid heap size". You can resolve this issue by running Solaris JVM with option -d64.


1 Answers

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:

enter image description here

like image 90
Vic Avatar answered Sep 23 '22 06:09

Vic