Solved

Chart heap leaking

Posted on 2011-03-11
9
439 Views
Last Modified: 2012-05-11
Please advise how to stop heap memory leaking in this chart:
public EURUSD_CandleStick_Market() {

        datesArrayList = new ArrayList();
        pricesArrayList = new ArrayList();

        interval = 60000L;
        af = new ApplicationFrame("Chart Market");
        csr = new CandlestickRenderer();

    }

    public void aflagc(boolean flag0c) {
        mflag0c = flag0c;
    }

    public void dsControl(boolean ds0) {
        mds = ds0;
    }

    public void eurusd_CandleStick_Market(double[] avg,
                                          java.util.Date[] avg_ts) {

        if (avg[0] != 0 && mds == true) {
            System.out.println("EUR/USD Market avg[0]    = " + avg[0]);
            System.out.println("EUR/USD Market avg_ts[0]    = " + avg_ts[0]);
            System.out.println("EUR/USD Market mds    = " + mds);
            System.out.println("EUR/USD Market mflag0c    = " + mflag0c);

            datesArrayList.add(avg_ts[0]);
            pricesArrayList.add(new Double(avg[0]));

            java.util.Date[] myDates =
                new java.util.Date[datesArrayList.size()];

            for (int j = 0; j < myDates.length; j++) {
                myDates[j] = (java.util.Date)datesArrayList.get(j);
            }

            double[] myPrices = new double[pricesArrayList.size()];
            for (int j = 0; j < myPrices.length; j++) {
                myPrices[j] = ((Double)pricesArrayList.get(j)).doubleValue();
            }

            //            System.out.println("Dates: ");
            for (int j = 0; j < myDates.length; j++) {
                //                System.out.println(myDates[j].toString());
            }
            //            System.out.println("Prices: ");

            for (int j = 0; j < myPrices.length; j++) {
                //                System.out.println(myPrices[j]);
            }
            System.out.println("interval: " + interval);

            DefaultHighLowDataset dataset =
                OHLCData(myDates, myPrices, myDates[0], interval);


            JFreeChart chart = createChart(dataset);
            ChartPanel chartPanel = new ChartPanel(chart);

            af.setContentPane(chartPanel);
            af.setSize(800, 500);
            af.setLocation(1600, 800);
            if (mflag0c == true) {
                af.setVisible(true);
                chartPanel.repaint();
            } else {
                af.setVisible(false);
            }
        }
    }

    public DefaultHighLowDataset OHLCData(java.util.Date[] dates,
                                          double[] prices,
                                          java.util.Date startDate,
                                          long interval) {
        ...........

        DefaultHighLowDataset dataset =
            new DefaultHighLowDataset("Series 1", finalDates, highs, lows,
                                      opens, closes, volumes);
        return dataset;
    }

    private JFreeChart createChart(final DefaultHighLowDataset dataset) {
        final JFreeChart chart =
            ChartFactory.createCandlestickChart("Candlestick EUR/USD Market",
                                                "Time", "Price", dataset,
                                                false);

        NumberAxis yaxis = (NumberAxis)chart.getXYPlot().getRangeAxis();
        yaxis.setAutoRangeIncludesZero(false);

        csr.setCandleWidth(10);
        chart.getXYPlot().setRenderer(csr);

        return chart;
    }
}

Open in new window

I have max heap 4096M but in 10 minutes work of this chart its depleted and stack this chart.
0
Comment
Question by:gbcbr
  • 5
  • 4
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35110951
I think we need tyo make JFreeChart chart and instance variable
and then check before  JFreeChart chart = createChart(dataset);
if (chart == null)

If it is null we should run this createChart method as we have it now,
if it is not null we should use setDataset method
 
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35111001
To do that you declare

JFreeChart chart;
ChartPanel chartPanel;


just after class declaration

and then wehere we have now

  JFreeChart chart = createChart(dataset);

instead of this line we put:

if(chart == null) {
chart = createChart(dataset);
  chartPanel = new ChartPanel(chart);

            af.setContentPane(chartPanel);
} else {
chart.getXYPlot().setDataset(dataset);
chartPanel.repaint();

}

Something along this line should work



0
 

Author Comment

by:gbcbr
ID: 35111038
I did it and start app, let's see the result
0
 

Author Comment

by:gbcbr
ID: 35111266
Looks like work!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 47

Expert Comment

by:for_yan
ID: 35111464
Good!
0
 

Author Comment

by:gbcbr
ID: 35111472
Thanks again!
0
 

Author Comment

by:gbcbr
ID: 35112251
Please check my question Open position calculation because in 40 minutes market will be close up to Monday and I can't check solution
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35112327
But I didn't understand how you receive this information and why you just count it when you recieve it
0
 

Author Comment

by:gbcbr
ID: 35112336
ask this questions and I will reply
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now