×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: out of memory

  1. #1
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Unhappy out of memory

    Code:
        public static Image scaleImage(Image src, int dstW, int dstH) {
            //scaling to create thumbnail
            //pottentioal out of memory.
            int srcW = src.getWidth();
            int srcH = src.getHeight();
            try {
                Image tmp = Image.createImage(dstW, srcH);
                Graphics g = tmp.getGraphics();
                int delta = (srcW << 16) / dstW;
                int pos = delta / 2;
                for (int x = 0; x < dstW; x++) {
                    g.setClip(x, 0, 1, srcH);
                    g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
                    pos += delta;
                }
                Image dst = Image.createImage(dstW, dstH);
                g = dst.getGraphics();
                delta = (srcH << 16) / dstH;
                pos = delta / 2;
                for (int y = 0; y < dstH; y++) {
                    g.setClip(0, y, dstW, 1);
                    g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
                    pos += delta;
                }
                return dst;
            } catch (Exception Ex) {
                return null;
            }
        }
    
        public static Image bytesToImage(byte[] data, boolean processAlpha) throws EOFException {
            //used when creating thumbnails
            //open byte->img->scale(img)->byte->base64(byte)->server
            Image img;
            try {
                ByteArrayInputStream bin = new ByteArrayInputStream(data);
                DataInputStream din = new DataInputStream(bin);
                int width = din.readInt();
                int height = din.readInt();
                int[] argb = new int[width * height];
                for (int i = 0; i < argb.length; i++) {
                    argb[i] = din.readInt();
                }
                img = Image.createRGBImage(argb, width, height, processAlpha);
            } catch (Exception e) {
                throw new EOFException("image data incomplete");
            }
            return img;
        }
    public static byte[] imageToBytes(Image img) {
            //scaled image to bytes
            int width = img.getWidth();
            int height = img.getHeight();
            try {
                int[] argb = new int[width * height];
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                DataOutputStream dout = new DataOutputStream(bout);
                img.getRGB(argb, 0, width, 0, 0, width, height);
                dout.writeInt(width);
                dout.writeInt(height);
                for (int i = 0; i < argb.length; i++) {
                    dout.writeInt(argb[i]);
                }
                return bout.toByteArray();
            } catch (Exception e) {
                // this won't happen, because we're writing to a ByteArrayOutputStream
                return "".getBytes();
            }
        }
    this code gives me an out of memory error
    pls tell me how can i avoid it by getting free and available memory.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory

    How big is the image?

  3. #3
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Re: out of memory

    image size may vary
    sometimes when image is large this throws error
    how can i capture this error?

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory

    Define "large". More information = more help. Give us some examples.

    You might be able to catch an OutOfMemoryError. However, CLDC implementations are not required to throw this error. They are permitted by the specification simply to shut down in an out-of-memory condition. Catching OutOfMemoryErrors cannot be relied upon across all devices.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Re: out of memory

    the size i think vary from devices.
    in my 5800 i have been able to scale an image(1280*800).
    but when i tried the same image in 5310 it gives outofmemory and application closes.

  6. #6
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Re: out of memory

    graham msgd u

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory

    Quote Originally Posted by ashwin.nath.m View Post
    in my 5800 i have been able to scale an image(1280*800).
    but when i tried the same image in 5310 it gives outofmemory and application closes
    When you use getRGB() on an image of this size, you create an int[] with the size 1280*800. That's 1,024,000 ints. A Java "int" is four bytes, so that requires 4,096,000 bytes of heap (that's just short of 4Mb). That's why your 5310, with its 2Mb heap, runs out of memory.

    Also, the Image objects themselves probably kill your memory. Check Tiger69's article in the wiki about memory usage of images.

    Graham.

  8. #8
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Re: out of memory

    Quote Originally Posted by grahamhughes View Post
    Check Tiger69's article in the wiki about memory usage of images
    link broken

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory

    Sorry: this one.

  10. #10
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Re: out of memory

    so how can i select images that can be resized using a device?
    my aim is to generate thumbnail of files in gallery

  11. #11
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory

    The best you can do is try to catch OutOfMemoryErrors.

    In general, Series 40 devices won't have enough memory to resize a photo-sized image.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: out of memory

    Quote Originally Posted by ashwin.nath.m View Post
    so how can i select images that can be resized using a device?
    my aim is to generate thumbnail of files in gallery
    Hi,

    Many JPEG images already have thumbnails in the EXIF data, have you considered using those? If a thumbnail is available and large enough for your needs, you can scale that and don't have to worry about size of the original image.

    -TongMuan

  13. #13
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Red face Re: out of memory

    i have used catch but then also app crashes in s40
    is there any way to avoid this?

  14. #14
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory

    In what way "crashes"?

  15. #15
    Regular Contributor
    Join Date
    Jan 2009
    Posts
    102

    Re: out of memory

    it gives a message
    "Out of Memory Error
    java/lang/OutOfMemoryError"
    and application quits.

Page 1 of 2 12 LastLast

Similar Threads

  1. memory jump while running game
    By TacB0sS in forum Mobile Java Games
    Replies: 21
    Last Post: 2009-06-20, 11:56
  2. Using Camera API requires too much memory
    By tote_b5 in forum Symbian Media (Closed)
    Replies: 18
    Last Post: 2008-07-31, 16:21
  3. Excessive midlet memory consumption
    By paulip in forum Mobile Java General
    Replies: 1
    Last Post: 2007-10-09, 19:24
  4. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29
  5. 9210 and 9210i java memory
    By allams in forum Mobile Java General
    Replies: 0
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •