Mihir
Mihir

Reputation: 2520

Java OutofMemory Strange Error

I have started my jar from command line using following command

java -Xms1200m -Xmx1500m -jar xxx.jar

and I am creating BufferedImage which is in size 12600 * 12600 means it requires

606 MB of memory in following code

TranscoderInput input = new TranscoderInput(sr);

            String pngFile = "Style-" + shoeViewer.getCurrentStyle() + "_"
                    + shoeViewer.getSelectedMetadata().getSizeLabel()
                    + "_400DPI" + ".png";

            File outputFile = new File(pngFile);

            FileOutputStream fo = new FileOutputStream(outputFile);

            TranscoderOutput output = new TranscoderOutput(fo);

            long now = System.currentTimeMillis();

            t.transcode(input, null);

when I am debugging memory usage before executing above lines , my Runtime shows

I need 606 MB of memory and 1100 MB is still free .

So when I run above codes after 34 mins it throws OutOfMemory Exception .

what is the problem ? is it process problem or heap problem ? why it takes 34 minutes ??

My PC configuration is following

Windows 32bit XP Service Pack2 Home Edition
Amd Athlon (tm) 7750 Dual Core 2.71 GHz
2GB of DDR2 RAM
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode, sharing)

please, help me solve out this problem.

below is the exception stack tarce with debugging messages.

before running above code i changed VM argument to following

java -Xms900m -Xmx1024m -XX:MinHeapFreeRatio=40 -XX:MaxHeapFreeRatio=70 -jar myjar.jar

Building XML Document from SVG Reader.... Build completed of XML Document from SVG Reader.... Converting modified XML Document to String Writer.... Conversion complete of modified XML Document to String Writer.... claimedBytes 766 Avaialbel Bytes 900189232 Memory Statastics------------------------ Total memory is bytes: 912326656 Total memory is megabytes: 870 Used memory is bytes: 9308384 Used memory is megabytes: 8 Freememory is bytes: 900189096 Free memory is megabytes: 858


transcoding started at :Sun Mar 24 11:09:59 IST 2013

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap spa
ce
        at java.awt.image.DataBufferInt.<init>(Unknown Source)
        at java.awt.image.SinglePixelPackedSampleModel.createDataBuffer(Unknown
Source)
        at java.awt.image.Raster.createWritableRaster(Unknown Source)
        at org.apache.batik.gvt.renderer.StaticRenderer.updateWorkingBuffers(Sta
ticRenderer.java:536)
        at org.apache.batik.gvt.renderer.StaticRenderer.repaint(StaticRenderer.j
ava:375)
        at org.apache.batik.gvt.renderer.StaticRenderer.repaint(StaticRenderer.j
ava:344)
        at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTran
scoder.java:111)
        at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstra
ctTranscoder.java:142)
        at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstra
ctTranscoder.java:156)
        at com.mmg.app.eventlistener.PlaceOrderActionListener.save(PlaceOrderAct
ionListener.java:302)
        at com.mmg.app.eventlistener.PlaceOrderActionListener.saveCanvas(PlaceOr
derActionListener.java:270)
        at com.mmg.app.eventlistener.PlaceOrderActionListener.actionPerformed(Pl
aceOrderActionListener.java:143)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)

Upvotes: 3

Views: 1228

Answers (1)

Ebbe M. Pedersen
Ebbe M. Pedersen

Reputation: 7488

Even through the raw unencoded image would only fill around 600 MB, the internal representation in the encoder might need more memory depending on how it's implemented. The following example shows how much memory is used when scaling a simple svg file to 12600 x 12600 (32 bit).

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class Test {
    public static void main(String[] args) throws Exception {
        PNGTranscoder t = new PNGTranscoder();
        t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(12600));
        t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(12600));
        FileInputStream fis = new FileInputStream("C:\\StackOverflow\\SVG-logo.svg");
        TranscoderInput input = new TranscoderInput(fis);
        OutputStream ostream = new FileOutputStream("C:\\StackOverflow\\res.png");
        TranscoderOutput output = new TranscoderOutput(ostream);

        System.out.println("AllocatedMemory: \t" + (Runtime.getRuntime().totalMemory() / 1024) + " Kb");
        t.transcode(input, output);
        System.out.println("AllocatedMemory: \t" + (Runtime.getRuntime().totalMemory() / 1024) + " Kb");

        ostream.flush();
        ostream.close();

    }
}

On my machine the output is:

AllocatedMemory:    4096 Kb 
AllocatedMemory:    1677721 Kb

So here the program uses 1677 MB to transcode a simple SVG file to a 12600 * 12600 PNG file.

Upvotes: 2

Related Questions