Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all
First all I am not an experienced programmer.
I tried to make a little program that predicts the statistical outcome of the game baccarat in the casino after a certain set of cards has already been drawn out of the deck.

Problem is now; I cannot simulate the program to run more then 25000 games, if it plays more then 25000 games of baccarat in a row it gives this error:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
I really do not know how to solve this error! Your help will be greatly appreciated.
I posted my code in the second post.
thanks!
 
nico samara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


[Added code tags to preserver formatting - Ilja]
[ November 28, 2004: Message edited by: Ilja Preuss ]
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
nico samara:

it gives this error:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
I really do not know how to solve this error! Your help will be greatly appreciated.
I posted my code in the second post.
thanks!


You probably have a memory leak somewhere. If you want to use code tags to format your listing, perhaps some one will come up with a more specific answer.

Alternatively, you might try to write it up as a singly threaded command line application rather than an AWT application, and see if you can get that running first.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Doesn't look like a performance question to me. Moving to "Java in General (intermediate)"...
 
nico samara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello

@Ilja : Thank you very much for your effort for retagging. I appreciate very much.


In my taskmanager I see that in the beginning the program uses 22000 MB ram and very quickly the ram usage goes up to 87000MB ram, after which the program gives the error. So it is obviously a memory leak. I am still trying everything. Garbage collection does not help....
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

Note that 87000 MB is 87 GB. It's extraordinarily doubtful your computer even has that much memory, and in any event, your standard JVMs won't handle more than 4GB. Much more likely that the task manager says 87 MB, which is about how much total memory a Sun JVM consumes with the default Java heap at the moment that you'll get an OutOfMemoryError.

Now, what's the problem with your program? All your computation is being done in the event handler for the "Deal Card" button. Event handlers are supposed to be very short, because as long as they're running, the GUI is blocked and nothing will be repainted, and other events won't be handled. But you've written this enormously long event handler which loops for a long time and makes many, many changes to the GUI's state. Every such change (i.e., every setText() call, etc) causes repaint events and other state-change events to be generated. But those events (each represented by a Java object) can't be handled, because your event handler is running! Therefore, they pile up. Eventually, the system event queue is full of these notification events, and you run out of memory.

To fix this, and to fix the GUI-updating problem you have as well, you have to do all this work in a separate thread. This is actually very easy:



and there you go.
 
nico samara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
@ Ernest Friedman-Hill , wow, great answer. Thank you very much! I will try this and let you know, but it already seems very logical what you are saying.

About the MB thing, I meant KB, sometimes I am a bit confused when writing fast...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
Came across this old thread since it's very similar to a problem I'm having.
I added the thread suggestion and it seems to resolve the memory problem, however its still causing me problems.
The difference in my implementation is that Im using a JComboBox as opposed to a JButton. The JComboBox will have values added onto it as per a search method (which looks at a SQL table and returns a resultset, and then adds one of the entries in that table as an item in the combobox).
After I added the thread suggestion, it works fine when i add and choose the first item onto the JComboBox...however, its causing problems when I add and choose a second item from the JComboBox. It seems to try and overwrite the result of choosing that second entry from the combobox.
Any ideas?
 
Ahmed Omer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Ahmed Omer:
Hi,
Came across this old thread since it's very similar to a problem I'm having.
I added the thread suggestion and it seems to resolve the memory problem, however its still causing me problems.
The difference in my implementation is that Im using a JComboBox as opposed to a JButton. The JComboBox will have values added onto it as per a search method (which looks at a SQL table and returns a resultset, and then adds one of the entries in that table as an item in the combobox).
After I added the thread suggestion, it works fine when i add and choose the first item onto the JComboBox...however, its causing problems when I add and choose a second item from the JComboBox. It seems to try and overwrite the result of choosing that second entry from the combobox.
Any ideas?



Ignore this. Due to my stupidity I used a while loop instead of a if statement which meant it keeps doing something it should've done once.
and to think I was starting to fear the realm of Threads!
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

I'm sorry, I don't speak english very well :-S (but if you want to speak french, I'll say: good idea ! )
I'll even try to introduce my problem to you:
I've this message : Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
after compilation of the following lines:
(In fact, it works with little pics, but not bigs (10MB), and I need it works with big pics)
Is there somebody who can explain or resolve this problem?
thank you very much !!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class zoomImage extends JFrame implements ActionListener {
private JLabel photoPreview;
//private JLabel photoPreview3;
private JSlider curseur = new JSlider(JSlider.VERTICAL, 1, 3, 1);
private BufferedImage image;
int width;
int height;

public zoomImage() {
super("");
build();
}

public void build() {
setTitle("debut du projet");
setSize(1200, 900);
setResizable(true);
setContentPane(buildContentPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JPanel buildContentPane() {
JPanel panel = new JPanel();
panel.setLayout(null);

photoPreview = new JLabel();
photoPreview.setBounds(100, 200, 200, 200);
panel.add(photoPreview);

//photoPreview3 = new JLabel();

JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Fichier");
JMenuItem zoomer = new JMenuItem("zoomer");
menu1.add(zoomer);
Ecouteur3 ec3 = new Ecouteur3();
zoomer.addActionListener(ec3);
JMenuItem ouvrir = new JMenuItem("Ouvrir");
menu1.add(ouvrir);
Ecouteur1 ec1 = new Ecouteur1();
ouvrir.addActionListener(ec1);
menuBar.add(menu1);
setJMenuBar(menuBar);

return panel;

}

class Ecouteur3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
Map map = new Map(image);
map.setVisible(true);
map.pack();
map.setTitle("Test");
}
catch (Exception ee) {
ee.printStackTrace();
}
}
}


class Ecouteur1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(zoomImage.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File f = fc.getSelectedFile();
image = ImageIO.read(f);
ImageIcon icz = new ImageIcon(creerMiniature(image));
photoPreview.setIcon(icz);
}
catch (IOException ee) {
ee.printStackTrace();
}
}
}
}


private static BufferedImage creerMiniature(BufferedImage image) {
int width = 200, height = 200;
BufferedImage result = new BufferedImage(width, height, image.getType());
Graphics2D g2d = result.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return result;
}

public class Map extends JFrame {

private MapPane mapPane;

private JSlider curseur = new JSlider(JSlider.VERTICAL, 1, 3, 1);

public Map(BufferedImage image) throws Exception {

//image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//image = ImageIO.read(new File("1.jpg"));
//image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

mapPane = new MapPane(image);

curseur.setMajorTickSpacing(1);
curseur.setMinorTickSpacing(1);
curseur.setPaintLabels(true);
curseur.setPaintTicks(true);
curseur.setSnapToTicks(true);
curseur.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {
JSlider curseur = (JSlider) e.getSource();
int scale = curseur.getValue();
mapPane.setScale(scale);
repaint();
}
});
setLayout(new BorderLayout());
add(new JScrollPane(mapPane), BorderLayout.CENTER);
add(curseur, BorderLayout.EAST);
}

private class MapPane extends JPanel {
private BufferedImage image;
private int scale = 1;

public MapPane(BufferedImage image) {
super();
setImage(image);

}

private void recalculateAndResetSize() {
int width = 1;
int height = 1;

Insets insets = getInsets();
width += insets.left + insets.right;
height += insets.top + insets.bottom;

if (image != null) {
width += image.getWidth() * scale;
height += image.getHeight() * scale;
}
Dimension size = new Dimension(width, height);
setSize(size);
setMinimumSize(size);
setPreferredSize(size);
}

public void setImage(BufferedImage image) {
this.image = image;
recalculateAndResetSize();
repaint();
}

public BufferedImage getImage() {
return image;
}

public void setScale(int value) {
this.scale = value;
recalculateAndResetSize();
repaint();
}

public int getScale() {
return scale;
}

@Override public void setBorder(Border border) {
super.setBorder(border);
recalculateAndResetSize();
}

@Override protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g2d = (Graphics2D) graphics;
if (image != null) {
Insets insets = getInsets();
Dimension size = getSize();
int panelWidth = size.width - (insets.left + insets.right);
int panelHeight = size.height - (insets.top + insets.bottom);
int scaledImageWidth = image.getWidth() * scale;
int scaledImageHeight = image.getHeight() * scale;
int x = (panelWidth - scaledImageWidth) / 2;
int y = (panelHeight - scaledImageHeight) / 2;

g2d.translate(insets.left, insets.top);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2d.drawImage(image, x, y, scaledImageWidth, scaledImageHeight, null);

g2d.translate( -insets.left, -insets.top);
}
}
}
}


public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
}

public static void main(String[] args) {
zoomImage fenetre = new zoomImage();
fenetre.setVisible(true);

}

}

 
Sylvain Prodo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have forgotten to explain the purpose of the programme:
I want to display a pic (a map in fact) and, by drag and drop, move this pic (as in mappy, or google map, etc...)
there arer some french words in the code:
ecouteur = listener
image = picture
creerMiniature = createLittlePic

later, when this problem will resolved, I'll try to zoom on the picture with the wheel of the mouse....I'm not out of the wood....
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Syl,

And welcome to Javranch!

Firstly we try not to wake up old threads here so can you please start a new one.

Choose the forum carefully, for your question I would post it in the AWT/Swing etc forum, you'll get the best responses there

Also please use the code tags when posting.

Thanks!
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ah I see you have already started a new thread, I will comment over there.
    Bookmark Topic Watch Topic
  • New Topic