Alright, my question is:How can I load around 100 - 125 .PNG's without using up alot of memory?

Information:I'm currently Loading 123 .PNG's (ImageIcon) on start up.(Added to a 'LinkedList<ImageIcon>')

I did notice that changing them from a 'BufferedImage' to a 'ImageIcon' while loading them reduced loading time and allocated memory, so i left them as ImageIcon's, also the same for ArrayList<ImageIcon> Vs.LinkedList<ImageIcon>.

It's just too much on my game though , not the loading process , but the actual allocated memory for those ImageIcon's.Eventually it will just crash my IDE or my Game.(Constantly rendering, updating, creating/processing objects, playing a animated background)All of the above ^ + the allocated memory of those ImageIcon's just tortures my computer.

Any help would be appreciated, possibly a better way to store the Images I.E cache.

And yes, OutOfMemory error does occur after a while, not all the time though o.E.Loading 123 .PNG's results in anywhere from 800mb to 1400mb of allocated memory (19% - 32% used).(Out of 4096mb I presume)

@Agro:I'm giving the feel of a 'animated background' by shuffling through the ImageIcon's loaded into the LinkedList. (Main Menu)I don't think I would want to dispose of any element in that LinkedList lol.Do believe that would result in me having to 'reload' that Image file

Why would you need all of these images in RAM at the same time? Anyway...

If you insist on loading everything...

The best solution to your problem is just to use a sprite sheet. Also, scaling the images down could really help save a lot of space in RAM.

There are other options though...

You could just try loading only the images you are using at the moment. Images in the user's hard drive aren't going anywhere. As long as you know the names to them, then just leave them there and reference them as needed. Usually, loading only images people can see is a very good way to make sure you don't overload the RAM.

but... again, "Why would you need all of these images in RAM at the same time?"

@ctomni231:I have to load all 123 of the Images before I get to the main menu of my game.Once at the main menu ALL of those 123 frames are going to be put to use, guaranteed constantly while in the main menu.

Lol, 'Sprite Sheet', that would defeat the purpose of the 'Screen Recorder' I programmed, used to obtain those 123 .PNG frames.

@ctomni231:I have to load all 123 of the Images before I get to the main menu of my game.Once at the main menu ALL of those 123 frames are going to be put to use, guaranteed constantly while in the main menu.

Lol, 'Sprite Sheet', that would defeat the purpose of the 'Screen Recorder' I programmed, used to obtain those 123 .PNG frames.

There are plenty of free tools to make a spritesheet from a group of images.

Since you're not using OpenGL, you may need to do some Google-ing to try and find animation compression techniques. You may have to write some ugly code if you want to get this working well.

Hmmm... so it is a slideshow of sorts, using a LinkedList to go through each image. Okay.

Well, you have an update loop for this, since I'm guessing you are going to want to show each of these 123 frames in a movie. I think chances are high that in the worst case scenario, you are going to want to show 1 frame per update loop...

Well, you are talking an ordered list. So you could just load a little chunk at a time, and delete the images that have already been seen. Have it work like a revolving door in order to keep your memory low. In this case, you'll probably only need to have one list with limited size.

Of course, there is always scaling down the images. That will help a lot.

Or, you might want to take a look into creating an animated .gif for that movie instead, though I'm unsure how well Java supports it. That will definitely shrink the size a lot if that is an option for you.

@ctomni231:Well, I was trying to load all of my frames at the beginning during the initial 'loading process' when the game is ran, to prevent from burdening the player with frequent loads/off loads of Images (Lag-spikes)

At the moment yes, 1 frame per update loop.(Hopefully frames are shuffled in intervals of 60 - 75ms, depending on the current sleep rate of the GameLoop).

EDIT: I haven't bothered to crop out unused pixels from the frames yet. (Intend too)

(Not In-Game Relevant)I should make a program that scans all 123 Images, stores all the pixel data of them, than generates a new image (Sprite Sheet) with all the images aligned with the correct spacing Than I'd only have to load 1 BufferedImage on start up + obtain 122 Sub Images right?Would that really 'optimize' / 'lower' the allocated memory? o.O

How large are the images? If you use a sprite sheet it would reduce the loading time greatly but that is all. If you have large images I strongly recommend dropping the size by 50% and then turn on rendering hints to use better quality scaling and just scale the images.

Personally, if you want to have a wicked animated background and do not want to use video files, it would probably be better to create it from 2-5 layered background images and a bunch of particles. This would drop memory size down substantial.

Drop java2D as soon as you can as you should be fine at about 2-5k draw calls depending on the cpu but if you plan on going higher you really need to use say libgdx or some other bindings. Not too hard to transition to. Draw calls being any form of graphics.draw.

@StumpyStrust:Size of the images range from 446kb to 667kb. Each of the Images are 1366x768. (Some unwanted pixels around the images, rendering positions compensate to remove)(Probably would be a good idea to scale them since my game is 800x600)In a post above I mentioned writing a program that could 'crop' out the unneeded pixels in each image, if I were to do that I could reduce the dimension of each image down to 1300x731, and knock off about 53kb.

I've messed around with JMonkey Engine, LWJGL, OpenGL etc.The main reason that all the game's / project I do are in pure Java2D is because my computer can't really handle 4000000000 triangles + shadows, movement etc lol (0 to 20FPS).

Thus i revert to JPanels and Java2D until I get a 'Programming / Gaming' laptop.

opengl will vastly out perform java2d. You can use opengl for just sprite batching. You do NOT need lighting, 3D, shaders, or any of that in order to get 5-10x performance. I have a tutorial on here for making a very fast spritebatcher using very little opengl. It is as simple as using graphics.drawImage() but much faster.

Your images are way, way to large. Put simply, unless you do some video streaming you are just kicking yourself in the teeth trying to use 100+ frames at that resolution. If you still want to do this then cut the size in half and drop out any unnecessary pixels. You don't need a program to do this. MS Paint can do it.

But seriously, 1-4 gig of ram? Games like Crysis don't take up that much. If it is that out of control I think you are doing more then just images wrong. Sound can bloat things if you don't do it right.

1.) determine the time it takes to load a single frame.2.) load 'x' number of frames before you start playing the "movie". Do some math to determine how many frames you need to ensure that you won't reach a point where the movie is waiting for an image to load.3.) clean up frames after they're played.

This might work if you don't need to store the entire thing in memory at all times, and it might speed up the load time.

If it takes roughly 200ms to load/unload an image, then that's roughly 5 images per second.

Saying that you had 123 images, that's about 25 images you'd need to load before you start swapping.

Then as you start the movie, you push images to the front of your list, and pop the images that have played in sort of a first in, first out sort of fashion.

This assumes that you aren't going to loop this movie continuously with zero pauses. If that's the case then there's no way around it and you need to have all the images or you'll run into a delay/crash somewhere along the line.

This is one of the few programming tasks that can't be done unless you use 'hacks'. Hacks are unreliable and only work in specific cases.

You need to work out a way to solve this problem with out using hacks.

There are 2 ways I can see to do so:

Use OpenGL + one of the LWJGL media players

Completely change your idea of the format, and use a system which contains whole images where neccessary, and when there are chunks of pixels that stay the same, have some code which only replaces the parts of the image that change (like using g.drawImage() on the original image). This will require a new file format, lots of designing, and some nasty coding work, but at least it will be reliable if you get it right, and it will cut down the size massively

Well Pickle, my Game's Dimension is only 800x600, so yeah lol.Those Images could be re sized some T_T.

EDIT:Lol guys.I'm not looking for a Game Engine to use..I'm not looking to upgrade from Java2D (At the moment)..I'm not having 'time' issues with loading them..I'm not looking to Stream JPEG from a website..

The opening question was:"How can I load around 123 Images while optimizing the allocated memory".

I lowered it by around 20MB just by changing the generic List<Type> that I was using.There's got to be some sort of compressed List<Type> that I haven't ran into yet..

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org