Java - Images and Memory

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Fimoreth
Posts: 6
Joined: Wed Jun 22, 2011 10:46 pm UTC
Location: Edmonton, Alberta

Java - Images and Memory

Postby Fimoreth » Wed Jun 29, 2011 5:10 am UTC

Good day,

I've been working on a Java Applet Game. My applet is very heavy with images and because of this, I've found that the applet is using a lot of memory. So much memory that it is no longer working on most OSX systems. I have tried running a garbage collector after my initial function, and I've been flushing images after they've been loaded. However, this hasn't reduced the memory enough to run on OSX. The majority of these images are .png.

I've been mostly using java.awt.Image files, though with the occasional BufferedImage for double-buffering and image rotation. I was wondering if I should change all of my Image types to BufferedImage or even Virtual Image for both better performance and improved memory usage. I have been running this through Google all day without any definite answer. I'll be trying out these other options on my own, but if someone else already has experience with this or else has any other advice, it would be greatly appreciated and should help me speed along to adding more to my game. If you require more information or even the applet code, just let me know.

Cheers,
James Finlay.

fuzzycuzzy
Posts: 120
Joined: Sun Apr 10, 2011 3:11 am UTC
Location: more of a state-of-mind really... hmm? what?
Contact:

Re: Java - Images and Memory

Postby fuzzycuzzy » Wed Jun 29, 2011 5:51 am UTC

wait, so let me get this straight, you're setting all your images to null after using them?

User avatar
Fimoreth
Posts: 6
Joined: Wed Jun 22, 2011 10:46 pm UTC
Location: Edmonton, Alberta

Re: Java - Images and Memory

Postby Fimoreth » Wed Jun 29, 2011 5:56 am UTC

I found through Google a site that said to do a <imagename>.flush() after I'm done with them. Honestly, I doubt this is doing anything since the code works by loading the image, placing it into an Animation class, and then I flush the original image. Really, it shouldn't do much to help since the images would no longer be used when the init() function is finished, except for the new value stored in the Animation class. At this point I'm trying whatever might help.

My real question is whether or not awt.Image is the best way to proceed for an image-heavy applet.

Edit: I figure out how to change the default heap size which seems to make it work on OSX. However, my question is still open to which Image type is generally the best to use in an image-heavy project.

transient
Posts: 65
Joined: Sat Jul 25, 2009 3:51 am UTC

Re: Java - Images and Memory

Postby transient » Thu Jun 30, 2011 3:30 am UTC

Just to make sure, are you loading each image only once? How many images are you talking about?

User avatar
Fimoreth
Posts: 6
Joined: Wed Jun 22, 2011 10:46 pm UTC
Location: Edmonton, Alberta

Re: Java - Images and Memory

Postby Fimoreth » Fri Jul 01, 2011 3:52 pm UTC

transient wrote:Just to make sure, are you loading each image only once? How many images are you talking about?


I am loading them only once, but I'd like to note that I am keeping the images inside classes for re-use. I've recently figured out how to run a memory management and it looks like the BufferedImages are the ones that are taking up all of my memory. I am loading a total of 18 images, the largest one being (2000x1500). After this, I have two bufferimages, along with some small temporary ones for image rotation. One bufferedimage is 2000x1500 while the other is the size of the screen 800x500.

Edit: Alright, through lots of research it looks to me like my main problem is that I'm holding on to those images. But, since I'm reusing them, would reloading them each time be efficient..?

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Java - Images and Memory

Postby Berengal » Sat Jul 02, 2011 4:25 am UTC

You know, rotated versions of images can be stored as a simple affine transformation. That could save you some space on those temporary BufferedImages.

Also images will naturally take a lot of memory, since they're usually "unpacked" into raw pixels in memory.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Java - Images and Memory

Postby RoadieRich » Sun Jul 03, 2011 12:13 am UTC

Another thing to consider is do you need to have the images that big? If only part of an image changes during an animation sequence, use the first frame as a (full-image) matt, and subsequent frames only contain the changes to the image.
73, de KE8BSL loc EN26.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests