dcsimg
How do you avoid java.lang.OutOfMemoryError when loading large image?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Benedict_Teoh
Posted On:   Friday, February 22, 2002 08:24 PM

I am trying to do some testing on view images via a Java program. However, when I tried loading a JPG file more than 500K, I encounter a "java.lang.OutOfMemoryError" Is there any way to control the memory so that I can avoid this error. I am running my program using Java 1.4.0 on Windows 2000 Pro with 130 Mbyte RAM. I have also tried running on Jdk 1.3.1 with the same effect. The problem does not happen if I load a smaller image. My code is as follows: import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class Alpha2a { private static String version = "0.03.0"; public static void   More>>

I am trying to do some testing on view images via a Java program.

However, when I tried loading a JPG file more than 500K, I encounter a "java.lang.OutOfMemoryError" Is there any way to control the memory so that I can avoid this error.

I am running my program using Java 1.4.0 on Windows 2000 Pro with 130 Mbyte RAM. I have also tried running on Jdk 1.3.1 with the same effect. The problem does not happen if I load a smaller image.

My code is as follows:



import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;



public class Alpha2a {

private static String version = "0.03.0";



public static void main(String[] args) {

Alpha2 a2 = new Alpha2();

DrawFrame frame = new DrawFrame();

frame.setTitle("Alpha2 - Version " + version);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.show();

}

}



class DrawFrame extends JFrame {

public DrawFrame() {

setTitle("Alpha2");

setSize(WIDTH,HEIGHT);



DrawPanel panel = new DrawPanel();

Container contentPane = getContentPane();

contentPane.add(panel);

}



public static final int WIDTH = 400;

public static final int HEIGHT = 400;

}



class DrawPanel extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

Font tFont = new Font("Helvetica", Font.PLAIN, 30);

Image tImage = Toolkit.getDefaultToolkit().getImage("c:/photos/Image-13.JPG");


double leftX = 100;

double topY = 100;

double width = 200;

double height = 150;


g.drawImage(tImage, 0,0,200,150,this);

}

}

   <<Less

Re: How do you avoid java.lang.OutOfMemoryError when loading large image?

Posted By:   Chandra_Patni  
Posted On:   Saturday, February 23, 2002 04:18 AM

The only way is to increase your heap size by using -Xmx256M (for example). The default heap size is 32M (almost certain).
About | Sitemap | Contact