1

I am using Jfreechart (jfreechart-1.0.13.jar) for the first time with struts1, java6, jboss4. I use this code to create the chart :

private JFreeChart getJfreeChart(int product, int msg) {

        DefaultPieDataset dpd = new DefaultPieDataset();
        dpd.setValue("product", product);
        dpd.setValue("msg", msg);

        JFreeChart chart = ChartFactory.createPieChart3D(null, dpd, true, false, false);

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSectionPaint("product", new Color(51, 102, 153));
        plot.setSectionPaint("msg", new Color(160, 218, 230));

        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} : {1}"));

        return chart;
}

In my action, I do like this to display the chart:

response.setContentType("image/png");
ServletOutputStream outputStream = response.getOutputStream();
ChartUtilities.writeChartAsPNG(outputStream, chart, 900, 450);
outputStream.close();

In my jsp, I use <img src="MyAction.do" /> to display the chart

When I execute getJfreeChart, There I notice a memory leak . Is there an anomaly in my code ?

java.lang.OutOfMemoryError: PermGen space
    at javax.swing.UIManager.initialize(Unknown Source)
    at javax.swing.UIManager.maybeInitialize(Unknown Source)
    at javax.swing.UIManager.getDefaults(Unknown Source)
    at javax.swing.UIManager.getColor(Unknown Source)
    at org.jfree.chart.JFreeChart.<clinit>(JFreeChart.java:261)
    at org.jfree.chart.ChartFactory.createPieChart3D(ChartFactory.java:763)
as_taous
  • 113
  • 3
  • 10
  • Running out of memory doesn't necessarily indicate a 'leak'. What is the size of your PermGen space set to? What else is running on that JVM instance? – Michael Jan 20 '15 at 10:41
  • @Mikaveli :I have this conf : -XX:PermSize=258M -XX:MaxPermSize=512M Only the application is running on the JVM instance. – as_taous Jan 20 '15 at 10:46

1 Answers1

0

First, try increasing your PermGen space. If it's a genuine leak, you'll still see the same error, but it should at least delay it.

Another option is to allow class unloading:

-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

However, this will have the effect of making Garbage Collection much slower, so isn't appropriate for a high-load system (and won't work for Java 7 onwards).

If you're performing repeated "hot deploys" in JBoss (or any other app server), try to avoid that and cold deploy instead (if possible). If not, that's obviously not going to be the cause of the problem.

If you do have a leak issue, the best option is to find the cause and resolve it. Use a tool like jmap to give you an idea for likely candidates causing the issue:

jmap -permstat <pid>

Docs: http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html

Also see this question: How to dump Permgen?

Finally, consider upgrading to Java 8 as it does away with the concept of PermGen space. Instead, data will either become part of the heap (such as interned Strings) or a new area called Metaspace - which is garbage collected by default.

Community
  • 1
  • 1
Michael
  • 7,348
  • 10
  • 49
  • 86
  • The increase of permGen space does not change anything; I have the same error (out of memory). I have also used these options `(-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled )` but unfortunately, the result is the same. I have used Java Visual VM, jmap, jhat to find the cause of the leak memory but for the moment, no result. It’s strange because sometimes I have not exceptions – as_taous Jan 20 '15 at 17:08
  • You'll need to be using the CMS garbage collector for those options to have an effect. See update regarding that and Java 8. – Michael Jan 21 '15 at 10:59