[Loading A 'small movie' in .PNG frames (Memory DEBO)] *closed*... (._.)

Well JGO, hoping somebody can help me further ‘memory optimize’ my latest project (not posted yet).

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>.

Pseudo code:
I’m currently loading them like so:


public void loadFrames() {
	frames = new LinkedList<ImageIcon>();
	while (totalFramesLoaded < totalFrames) {
		if (frameLoadingComplete) break;
		frames.add(new ImageIcon("res/frames/export "+totalFramesLoaded+".PNG"));
		totalFramesLoaded++;
		/* Repaint our UI since our other Thread for doing this is currently paused */
		progressBar.setValue(totalFramesLoaded);
		getGamePanel().renderLoadingScreen();
	}
}

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.

Huh, this is a toughie. :confused:

I don’t think it would be that good, but try loading the image displaying it, disposing of it, and go onto the next one. I can’t really think of a good way to do in Java…

Are you getting OutOfMemoryErrors? Or just performance problems but the program runs? If the former, just increase the maximum heap size:

java -Xmx1024m ...

@BoBear2681:
Current VM Arguments:


-Xms64m -Xmx4096m

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.
:frowning:

You might want to look into one of the LWJGL movie players. (You are using opengl right?)
Videos are compressed quite well, so that may be the way to go.

Here is an article on video compression: http://www.axis.com/files/whitepaper/wp_videocompression_33085_en_0809_lo.pdf

With this, you may be able to write something that only changes certain pixels each frame, therefore minimizing the amount of new data per frame.

If you don’t understand this, just stick with my first suggestion. Otherwise things may get messy.

No OpenGL lol. :o :o :o :o

Graphics2D/JPanel, steady @ 100FPS. :point:

Main Menu: (In Controls Menu, yes the background’s animated)

Loading Screen: (123 frames already loaded, now loading other stoofs)

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 :o :o
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. :expressionless:
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.

EDIT: Whenever I use Graphics2D / Graphics, I always enable 9 certain RenderingHints :wink:

4000000000 triangles is way more than you will need, and way more than Java2D will ever be able to run at X fps.
Any opengl binding is going to give you far better performance than Java2D.

Your ‘facts’ need to be revised.

The 400000000000000 was just my index finger fully relaxed for a second my friend, didn’t state it as a fact :o
Like I can remember the exact number of vertices w.e lol 12:34AM.

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.

123 * 1366 * 768 * 4 = 492,24MBs. Doesn’t seem that bad actually.

The images are stored uncompressed, so how big the compressed files are is irrelevant. The only thing that matters is the bit depth and the size of them.

Maybe you can make it read only the first 10 images or so. Then it reads the images as they are required and discards already rendered images when they aren’t needed anymore.

fyi half-life 2 needs 256MB ram

[quote]A ‘small movie’ in .PNG frames
[/quote]
No. Just no.

Using Java2D, I recommend just the old crappy JMF, and then using a MJPEG video file…

No. Just no.

Using Java2D, I recommend just the old crappy JMF, and then using a MJPEG video file…
[/quote]
Uuugh… Not that nightmare again…

Just a suggestion, I haven’t even tested this…but.

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.

Just a suggestion :slight_smile:

-Pickle