0

I am using JGraphX to diagram workflows of varying size. I am encountering a problem that once I hit a certain number of cells, the mxCellRenderer method createBufferedImage() fails with an OutOfMemoryError. JGraphX renders the diagram properly in the Swing JFrame, regardless of whether it saves properly or not.

Is there a known work-around or modification to the class and/or methods to allow for large images?

Example code:

public void generatePNG() throws IOException {
    BufferedImage image = mxCellRenderer.createBufferedImage(graph, null, 1, Color.WHITE, true, null);
    ImageIO.write(image, "PNG", new File(path + "-" + name + ".png"));
}

Resulting stacktrace:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(Unknown Source)
at java.awt.image.Raster.createPackedRaster(Unknown Source)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at com.mxgraph.util.mxUtils.createBufferedImage(Unknown Source)
at com.mxgraph.util.mxUtils.createBufferedImage(Unknown Source)
at com.mxgraph.canvas.mxImageCanvas.<init>(Unknown Source)
at com.mxgraph.canvas.mxImageCanvas.<init>(Unknown Source)
at com.mxgraph.util.mxCellRenderer$1.createCanvas(Unknown Source)
at com.mxgraph.util.mxCellRenderer.drawCells(Unknown Source)
at com.mxgraph.util.mxCellRenderer.createBufferedImage(Unknown Source)
at com.mxgraph.util.mxCellRenderer.createBufferedImage(Unknown Source)

This same method functions perfectly fine for "smaller" diagrams (including some rather large ones) but once it reaches a certain limit no amount of increasing heap space appears to help.

The only other question I've found related to this is an older one which didn't really have a satisfactory answer: Jgraphx out of memory - Java

Any help would be appreciated!

  • 1
    Possible duplicate of [How can I increase the JVM memory?](https://stackoverflow.com/questions/2294268/how-can-i-increase-the-jvm-memory) – Leonardo Alves Machado Jul 10 '17 at 19:31
  • "no amount of increasing heap space appears to help" Here's my current JVM config: -Xms512m -Xmx8192m – Michael Coxson Jul 10 '17 at 21:44
  • Out of curiosity, what empirical size threshold, in pixels, have you encountered? Any way to make it create a more densely packed [image](https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html), e.g. `TYPE_BYTE_BINARY` or `TYPE_BYTE_INDEXED`? More [here](https://stackoverflow.com/q/9089675/230513). – trashgod Jul 10 '17 at 22:04
  • I've decided to go about it a different route and automatically clip the diagram if its mxRectangle exceeds a specific bounds and plot the top-level objects as separate diagrams. Beyond a certain point a huge diagram becomes unmanageable anyhow, so perhaps it's for the best. – Michael Coxson Jul 11 '17 at 20:02

1 Answers1

0

Maybe you could try to set antialias to false. That works for me.