我正在尝试从JAVA中创建的mxgraph创建一个mxgraph和一个图像。下面是从mxgraph创建图像的代码。
BufferedImage image = mxCellRenderer.createBufferedImage(graph,
null, 1, Color.WHITE, graphComponent.isAntiAlias(), null,
graphComponent.getCanvas());
// Creates the URL-encoded XML data
mxCodec codec = new mxCodec();
String xml = URLEncoder.encode(mxXmlUtils.getXml(codec.encode(graph.getModel())), "UTF-8");
mxPngEncodeParam param = mxPngEncodeParam.getDefaultEncodeParam(image);
param.setCompressedText(new String[] { "mxGraphModel", xml });
//Saves as a PNG file
outputStream = new FileOutputStream(new File("graph.jpg"));
ImageIO.write(image, "jpg", outputStream);
outputStream.close();
image = null;
我在图表中使用分层布局。
但是我在为更大的图形创建图像时遇到内存不足错误。
如何摆脱这个内存问题(除了增加堆大小)? 有没有其他方法可以解决这个问题(除了增加堆大小)?
答案 0 :(得分:1)
在此处查看此帖:
http://forum.jgraph.com/questions/5408/save-as-png-detect-out-of-memory
尤其是底部。 JGraphX中有一个检查确定是否有足够的内存。那是错的。可能没有足够的内存,因为GC还没有运行。如果GC运行,则释放内存并且createBufferedImage方法可以成功。因此,不应检查空闲内存,而是应该在try {...} catch(错误错误} {...}块中分配内存。