Jgraphx out of memory - Java

你说的曾经没有我的故事 提交于 2019-12-13 00:45:02

问题


I am trying to create an mxgraph and a image from the created mxgraph in JAVA. Below is the code to create the image from 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;

I am using hierarchical layout in the graph.

But I am getting the out of memory error on creating the image for larger graph.

How can i get rid of this memory issue (apart from increasing the heap size)? Is there any other alternate way to solve this problem (apart from increasing the heap size)?


回答1:


See this post here:

http://forum.jgraph.com/questions/5408/save-as-png-detect-out-of-memory

especially the bottom part. There's a check in JGraphX which determines if there's enough memory. That one is wrong. There may not be enough memory because the GC hasn't run yet. If the GC runs, then memory would be freed and the createBufferedImage method could be successful. So instead of checking for the free memory, the memory should have just been allocated in a try { ... } catch( Error err} { ... } block.



来源:https://stackoverflow.com/questions/19295127/jgraphx-out-of-memory-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!