Memory problem with displaying images

Discussion in 'Java' started by k.mucha4@chello.pl, Mar 6, 2007.

  1. Guest

    Hi,

    I've got a problem: I want to write an application with 6 JButtons.
    Clicking on each JButton should display an appriopriate image. Images
    are .jpg files (1000x1000 pixels).

    Now, I've got something like that:

    URL img1Url, img2Url, img3Url, img4Url, img5Url, img6Url;
    JLabel img1, img2, img3, img4, img5, img6;

    img1Url = getClass().getResource("img1.jpg");
    img2Url = getClass().getResource("img2.jpg");
    img3Url = getClass().getResource("img3.jpg");
    img4Url = getClass().getResource("img4.jpg");
    img5Url = getClass().getResource("img5.jpg");
    img6Url = getClass().getResource("img6.jpg");

    img1 = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(img1Url)));
    img2 = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(img2Url)));
    img3 = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(img3Url)));
    img4 = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(img4Url)));
    img5 = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(img5Url)));
    img6 = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(img6Url)));

    After that, I add all JLabels to JPanel and do setVisible() on each
    JLabel using JButtons.

    But when I create 6 JLables at one time, the memory usage increase up
    to 22MB(!!!). If I wanted to use 20 images, the out of memory error is
    sure.

    So how should I build such application? What should I do to load into
    memory only one image, which is currently displayed?

    Please, help me.
     
    , Mar 6, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I've got a problem: I want to write an application with 6 JButtons.
    > Clicking on each JButton should display an appriopriate image. Images
    > are .jpg files (1000x1000 pixels).
    >
    > Now, I've got something like that:
    >
    > URL img1Url, img2Url, img3Url, img4Url, img5Url, img6Url;
    > JLabel img1, img2, img3, img4, img5, img6;
    >
    > img1Url = getClass().getResource("img1.jpg");
    > img2Url = getClass().getResource("img2.jpg");
    > img3Url = getClass().getResource("img3.jpg");
    > img4Url = getClass().getResource("img4.jpg");
    > img5Url = getClass().getResource("img5.jpg");
    > img6Url = getClass().getResource("img6.jpg");
    >
    > img1 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img1Url)));
    > img2 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img2Url)));
    > img3 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img3Url)));
    > img4 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img4Url)));
    > img5 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img5Url)));
    > img6 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img6Url)));
    >
    > After that, I add all JLabels to JPanel and do setVisible() on each
    > JLabel using JButtons.
    >
    > But when I create 6 JLables at one time, the memory usage increase up
    > to 22MB(!!!). If I wanted to use 20 images, the out of memory error is
    > sure.
    >
    > So how should I build such application? What should I do to load into
    > memory only one image, which is currently displayed?
    >
    > Please, help me.
    >


    Don't load the images until you need them, e.g.:

    JLabel image;
    public void showImage(URL url) {
    image = new JLabel(new
    ImageIcon(Toolkit.getDefaultToolkit().createImage(url)));
    }

    The garbage collector will free up the unused images in the meantime.
    (If you want to be doubly sure, call Runtime.gc())
     
    Joshua Cranmer, Mar 6, 2007
    #2
    1. Advertising

  3. Daniel Pitts Guest

    On Mar 6, 1:11 pm, wrote:
    > Hi,
    >
    > I've got a problem: I want to write an application with 6 JButtons.
    > Clicking on each JButton should display an appriopriate image. Images
    > are .jpg files (1000x1000 pixels).
    >
    > Now, I've got something like that:
    >
    > URL img1Url, img2Url, img3Url, img4Url, img5Url, img6Url;
    > JLabel img1, img2, img3, img4, img5, img6;
    >
    > img1Url = getClass().getResource("img1.jpg");
    > img2Url = getClass().getResource("img2.jpg");
    > img3Url = getClass().getResource("img3.jpg");
    > img4Url = getClass().getResource("img4.jpg");
    > img5Url = getClass().getResource("img5.jpg");
    > img6Url = getClass().getResource("img6.jpg");
    >
    > img1 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img1Url)));
    > img2 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img2Url)));
    > img3 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img3Url)));
    > img4 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img4Url)));
    > img5 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img5Url)));
    > img6 = new JLabel(new
    > ImageIcon(Toolkit.getDefaultToolkit().createImage(img6Url)));
    >
    > After that, I add all JLabels to JPanel and do setVisible() on each
    > JLabel using JButtons.
    >
    > But when I create 6 JLables at one time, the memory usage increase up
    > to 22MB(!!!). If I wanted to use 20 images, the out of memory error is
    > sure.
    >
    > So how should I build such application? What should I do to load into
    > memory only one image, which is currently displayed?
    >
    > Please, help me.


    The 22MB might not be the actual active memory. Don't assume you are
    going to run out of memory unless you actually do.

    Instead of just guessing that an "out of memory error is sure [sic]",
    try it with 20 images and see how much your memory increases.

    In any case, if you are only displaying one image at a time, it
    wouldn't hurt to only load one image at a time.

    BTW, your example would be better written:

    JLabel[] labels = new JLabel[6];
    for (int i = 0; i < 6; ++i) {
    String fileName = "img" + i + ".jpg";
    URL url = getClass().getResource(fileName);
    labels = new JLabel(new ImageIcon(url));
    }
     
    Daniel Pitts, Mar 7, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David J. Berman
    Replies:
    0
    Views:
    1,440
    David J. Berman
    Jun 24, 2003
  2. Matt
    Replies:
    0
    Views:
    398
  3. Vinay

    Displaying Images ?

    Vinay, Oct 24, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    547
    vMike
    Oct 25, 2003
  4. Cloud Burst
    Replies:
    8
    Views:
    619
    Gary Mc
    Jan 14, 2004
  5. David
    Replies:
    2
    Views:
    603
    David
    Mar 5, 2004
Loading...

Share This Page