Urgent - java.lang.OutofMemoryError:Java heap space


Hi ,

Pls help,

When i run the codes i get errror:

exception in thread "AWT- EventQueue -1" java.lang.OutofMemoryError:Java heap space.

What do i do ?
Jasbir21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
Hi Jasbir21,

There must be an instant loop, which creates big objects, You should check Your code, or Your application is so heavy, that it uses the memory JVM is allowed to use. To increase the amount of JVM memory, pass an argument to Your java an argument:

java -Xmx256M ...

this sets the ammount of memory to 256MB, while by default java uses 64MB
CEHJCommented:
You may be creating infinite recursion, such as when a method is called in a listener that causes that listener to receive events
Jasbir21Author Commented:
hi,

actually i am using applets...when i try appletviewer -Xmx256M example.html

had error : unsupported option : - Xmx256M
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

StillUnAwareCommented:
Do this:

appletviewer -J-Xmx256M
CEHJCommented:
You can't pass options to appletviewer like that. It's unlikely to help anyway if there are errors
Jasbir21Author Commented:
i am using a for loop where the number of iterations can go up to 500000.

-->>>
appletviewer -J-Xmx256M example.html

if there a way to embede this command in the code or html file. because if the user is not using appletviewer , use browser (internet explorer)?

thanks
CEHJCommented:
>> am using a for loop where the number of iterations can go up to 500000.


That's of little consequence. What you're *doing* in it likely *is*
CEHJCommented:
>>if there a way to embede this command in the code or html file.

AFAIK no. Did it 'solve' the problem anyway?
Jasbir21Author Commented:
i am drawing a scatter graph ..


                  for (int i = 0; i < iterations; i++)
                  {  
                        series = new XYSeries("S" + i);
                        for (int j = 0; j < years+1; j++)
                        {  
                              series.add(j, score[j][i]);
                                                      }
                        dataset.addSeries(series);
                  }
Jasbir21Author Commented:
actually, with appletviewer (appletviewer -J-Xmx256M example.html )

, it solve, but when i put on server..

i cannot use appletviewer .. i need to use internet explorer...

thanks

CEHJCommented:
Hmm. That's a third party library i take it, so that makes things trickier
Jasbir21Author Commented:
yup..jfreegraph
CEHJCommented:
You'll need to either hand-optimise the code or use a different library probably
Jasbir21Author Commented:
what do you mean by  ->>You'll need to either hand-optimise the code

thanks
CEHJCommented:
Look at their source to see if you can optimise it
Jasbir21Author Commented:
but is there a way to allocate jvm memory dynamically in the code?
The code runs well up to iteration of 7000 .

Once it was able to be runned up to 20 000 . But when i tried again, got the memory error..thanks

CEHJCommented:
>> but is there a way to allocate jvm memory dynamically in the code?

No, not practicably

Their code may be buggy. Try another library. JFreeChar?
CEHJCommented:
JFreeChar=JFreeChart
Jasbir21Author Commented:
actually, i need to use jfreechart .  
Jasbir21Author Commented:
one of the requirements from the lecturer

for (int i = 0; i < iterations; i++)
               {  
                    series = new XYSeries("S" + i);
                    for (int j = 0; j < years+1; j++)
                    {  
                         series.add(j, score[j][i]);
                                             }
                    dataset.addSeries(series);
               }

- is there a way to disallocate memory after this loop..so that when it run again,
memory can be reused .thanks
CEHJCommented:
>>- is there a way to disallocate memory after this loop..so that when it run again,

No, that is done by the garbage collector and it's only advisory to call gc. You may try it though

System.gc();
Thread.sleep(50);
AmodKrCommented:
You can try with WeakReference
Jasbir21Author Commented:
i still get the out of memory error ...

--->>
You can try with WeakReference  ... what do you mean by that ? thanks
CEHJCommented:
>> actually, i need to use jfreechart .  

If this is the case, why a you still concerning yourself with another one?
Jasbir21Author Commented:
oh..ic..Weak Reference is a third party library as well ?

i thought , maybe putting a reference or somehing in side..
CEHJCommented:
A WeakReference could only conceivably be of use to you if you *were* using it in *their* code
StillUnAwareCommented:
For a specific machine (on Windows) You can set the memory amount to 256MB to be used in browser this way:

in Control Panel find Java applet, there in "Java" -> "Java Applet Runtime Settings" -> "View..." -> "JRE Settings" set the "Java Runtime Parameters" field to "-Xmx256M" or "-J-Xmx256M". You'll have to find out which one of two is correct.
AmodKrCommented:
for (int i = 0; i < iterations; i++)
               {  
                    WeakReference reference = new WeakReference(new XYSeries("S" + i));
                    XYSeries series = (XYSeries)reference.get();
                    for (int j = 0; j < years+1; j++)
                    {  
                         series.add(j, score[j][i]);
                                             }
                    dataset.addSeries(series);
               }
I am not sure that it ll help u or not, but u can give it a shot.
AmodKrCommented:
Why you need to loop say 50 000 times , all together? Why can't you divide the iteration?? Say putting part by part to the garph.
Changing the JRE settings to gain a solution is game.
Jasbir21Author Commented:
--->

-> "Java Applet Runtime Settings" -> "View..." -> "JRE Settings" set the "Java Runtime Parameters" field to "-Xmx256M" or "-J-Xmx256M".

I am still gettingout of memory error ...

--->
 WeakReference reference = new WeakReference(new XYSeries("S" + i));
it does not recognise the weakreference symbol...

thanks
AmodKrCommented:
import java.lang.ref.WeakReference;
Giant2Commented:
Are you sure that there is not an infinite loop in your code?
Jasbir21Author Commented:
hi,

i dont think there is  any infinite loop . it works with 5000...

how do i put it in on the graph part by part...

  if(i<5000)

series = new XYSeries("Iteration" + i);

          else      
       ser=new XYSeries("DS" + i);

is it something like this? thanks
Giant2Commented:
>i dont think there is  any infinite loop . it works with 5000...

So you really use much memory. You need to use less memory. For example you must check if you create variable not really needed or duplicated.
Jasbir21Author Commented:
actually, the codes work up to 10000 when i try with line graph...
it only happens when i try with scatter graph
CEHJCommented:
Are you using the data points for any other purpose than displaying them?
Jasbir21Author Commented:
just for displaying them..thanks
CEHJCommented:
What API are you now using - i'm rather confused now
Jasbir21Author Commented:
swingx and awt..

and jfreegraph
Jasbir21Author Commented:
jfreegraph has the  methods/classes to display them ..

............

series.add(j, score[j][i]);
                                             }
                    dataset.addSeries(series);
----------------- like this is to add
CEHJCommented:
What's the fully-qualified name of the main chart class you're using?
Jasbir21Author Commented:
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
                domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());


import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
Jasbir21Author Commented:
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
CEHJCommented:
I don't know this API but it may be a good idea for you to post the code that is causing the problem anyway
Jasbir21Author Commented:
for (int i = 0; i < iterations; i++)
               {  
                    series = new XYSeries("S" + i);
                    for (int j = 0; j < years+1; j++)
                    {  
                         series.add(j, score[j][i]);
                                             }
                    dataset.addSeries(series);
               }
this is the code to add scores...

and the codes below is to display them..

//create a chart to display the competency range
                chart = ChartFactory.createScatterPlot(
                  "Example",    
                  "X ",            
                  "Y",    
                  dataset,                  // data
                  PlotOrientation.VERTICAL,
                  false,                    
                  true,                    
                  false                    
                  );
        
                  XYPlot plot = (XYPlot) chart.getPlot();

                  XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
                  renderer.setShapesVisible(true);
                  renderer.setBaseShapesFilled(false);
            
                  plot.setRenderer(renderer);
            
            
                  chartPanel=new ChartPanel(chart);
Giant2Commented:
>               XYPlot plot = (XYPlot) chart.getPlot();
>
>               XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
>               renderer.setShapesVisible(true);
>               renderer.setBaseShapesFilled(false);
>         
>               plot.setRenderer(renderer);

cycle risk.


>for (int i = 0; i < iterations; i++)
>...
>for (int j = 0; j < years+1; j++)
>...

too many object.
CEHJCommented:
How is 'iterations' set?
Jasbir21Author Commented:
the user can enter the number of iterations....
CEHJCommented:
You may need to limit that value. Try a low value and increase until the error occurs. *Don't* mix it up with your current GUI problems

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jasbir21Author Commented:
ok..got the error..what should i do now ?
Giant2Commented:
If you use many object the only thing you can do is limit the number like CEHJ told or (if there aren't loops) find another way to consume less memory.
Jasbir21Author Commented:
thanks a lot :-)
CEHJCommented:
:-)
StillUnAwareCommented:
glad to help
AmodKrCommented:
Glad to hear that u r through :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.