Results 1 to 5 of 5
  1. #1
    NuxlyStardust is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Question Using the same image several times: does it eat up more RAM?

    Hello there :)
    I have a question about how images loaded with java.awt.Image are managed internally.

    Here's what I'm doing:
    I'm writing a Swing component that will be used several times at once because it will appear inside a child window in a MDI setup. And this component loads a PNG image from the disk to draw it as a background.
    And I'm not sure how to go with that...
    Because if I load that image from the constructor and keep the reference it might load one image in the memory for each window and thus eat up a lot of RAM. Unless Java takes care of making sure that images are loaded only once, but I'm not sure it does so I hoped you could tell me.

    The other way I'm considering is to have a ResourceManager singleton class that takes care of loading all the images that my program will need, and store them in a HashMap so they can be picked up anywhere in the code using a string identifier.

    What would be the best design?

    Thanks :)

  2. #2
    NuxlyStardust is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Any idea? :s

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    Have you done any profiling at all? Your first question is best answered by writing a program and testing it yourself- feel free to post it in SSCCE form. Let us know when you do that, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    What do you mean by
    images loaded with java.awt.Image
    since that doesn't make any sense to me.

    Unless Java takes care of making sure that images are loaded only once
    A static variable is created once and once only, when the class is loaded.

    db

  5. #5
    NuxlyStardust is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    What do you mean by

    since that doesn't make any sense to me.
    I mean ImageIcon, and I keep it in an Image object.
    So using a static variable would be the "standard way" to do this?
    Cause if I need it in another class it will exist twice in the memory...

Similar Threads

  1. Looping WAY too many times...
    By BariMutation in forum New To Java
    Replies: 10
    Last Post: 10-15-2010, 10:27 AM
  2. Times without dates.
    By JavaJuJitZu in forum Advanced Java
    Replies: 14
    Last Post: 01-17-2010, 10:54 PM
  3. My buttons all perform their action like 100 times
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 10:49 AM
  4. Difference in seconds between two times
    By jazzy639 in forum New To Java
    Replies: 11
    Last Post: 09-26-2009, 06:58 PM
  5. Replies: 3
    Last Post: 10-02-2008, 05:48 AM

Posting Permissions

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