Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2 3
  ignore  |  Print  
  [Loading A 'small movie' in .PNG frames (Memory DEBO)] *closed*... (._.)  (Read 5250 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-01-17 05:08:24 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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 Undecided, not the loading process Wink, 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.

Online Agro
« Reply #1 - Posted 2013-01-17 05:10:21 »

Huh, this is a toughie. :/

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

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #2 - Posted 2013-01-17 05:16:50 »

Are you getting OutOfMemoryErrors?  Or just performance problems but the program runs?  If the former, just increase the maximum heap size:
1  
java -Xmx1024m ...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2013-01-17 05:17:55 »

@BoBear2681:
Current VM Arguments:
1  
-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 Angry

Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #4 - Posted 2013-01-17 05:33:44 »

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?"

Offline GabrielBailey74
« Reply #5 - Posted 2013-01-17 05:37:17 »

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

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #6 - Posted 2013-01-17 05:45:36 »

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.

Offline GabrielBailey74
« Reply #7 - Posted 2013-01-17 05:46:58 »

No OpenGL lol. Shocked Shocked Shocked Shocked

Graphics2D/JPanel, steady @ 100FPS. Pointing

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


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

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #8 - Posted 2013-01-17 05:50:08 »

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

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.

Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #9 - Posted 2013-01-17 05:53:15 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #10 - Posted 2013-01-17 06:08:45 »

@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 Shocked Shocked
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

Offline StumpyStrust
« Reply #11 - Posted 2013-01-17 07:18:58 »


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.


Offline GabrielBailey74
« Reply #12 - Posted 2013-01-17 07:36:28 »

@StumpyStrust:
Size of the images range from 446kb to 667kb. Stare
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

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #13 - Posted 2013-01-17 09:02:20 »

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.

Offline GabrielBailey74
« Reply #14 - Posted 2013-01-17 09:32:32 »

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



Offline StumpyStrust
« Reply #15 - Posted 2013-01-17 10:39:39 »

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.

Offline theagentd
« Reply #16 - Posted 2013-01-17 13:53:27 »

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.

Myomyomyo.
Offline Cero
« Reply #17 - Posted 2013-01-17 17:06:11 »

fyi half-life 2 needs 256MB ram


Quote
A 'small movie' in .PNG frames
No. Just no.

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

Offline theagentd
« Reply #18 - Posted 2013-01-17 17:24:22 »

fyi half-life 2 needs 256MB ram


Quote
A 'small movie' in .PNG frames
No. Just no.

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

Uuugh... Not that nightmare again...

Myomyomyo.
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #19 - Posted 2013-01-17 17:29:43 »

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 Smiley


-Pickle

Offline GabrielBailey74
« Reply #20 - Posted 2013-01-17 22:19:03 »

Thanks pickle my issue isn't relating to the loading process Pointing
The loading process loads 123 frames almost flawlessly while updating a JProgressBar's percentage.

I know there's got to be a better way to cache Images lol..

If by changing them from a:
1  
ArrayList<BufferedImage>

To:

1  
LinkedList<ImageIcon>

reduced the allocated memory on start up from 872mb to 850mb, what else can I do?

Your 3 steps sound like something I'll try out today, I know it takes anywhere from 200ms - 216ms to load 1 frame.
(Shuffled in intervals of 60ms - 75ms while playing)

Now that I think about it this, it isn't really efficient lol.
You don't see YouTube 'pre-buffering' a whole clip at once when you click play XD.

Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #21 - Posted 2013-01-17 22:31:33 »

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.


-Pickle Smiley

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #22 - Posted 2013-01-17 22:52:02 »

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

Good Luck!

Offline Cero
« Reply #23 - Posted 2013-01-17 22:55:49 »

fyi half-life 2 needs 256MB ram


Quote
A 'small movie' in .PNG frames
No. Just no.

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

Uuugh... Not that nightmare again...

yea ^^
but its just appropriate when you wanna play video in java 2D  D:
the punishment should fit the crime :D

Offline GabrielBailey74
« Reply #24 - Posted 2013-01-17 23:12:30 »

My bad T_T

1  
ArrayList<BufferedImage> / LinkedList<BufferedImage> 
results in 850mb used.

1  
ArrayList<ImageIcon> / LinkedList<ImageIcon>
is the one resulting in 872mb used.

(After research):
Evidently my problem has to due with a 'Memory Leak', which is being caused by me adding all the Images to the LinkedList and never removing them O.O.

I tried the 'Memory leak finder / stopper' called "Plumbr" that thing failed hard-core lol.
Never even detected the memory leak, as soon as it occurred Plumbr just crashed Stare Stare Stare

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #25 - Posted 2013-01-17 23:19:13 »

850MB is more than my HexPrism engine uses (mostly)

Just use OpenGL. There is absolutely no advantage to Java2D except the easyness of learning.
Once you've learnt the basics of OpenGL, it's even easier to code.

Offline nsigma
« Reply #26 - Posted 2013-01-17 23:19:26 »

You don't need to use JMF to play a video-only MJPEG stream. You can do that with just ImageIO. Various examples online, such as this http://thistleshrub.net/Joomla/index.php?option=com_content&view=article&id=115:displaying-streamed-mjpeg-in-java&catid=43:robotics&Itemid=64

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #27 - Posted 2013-01-17 23:24:53 »

That really does make sense... since 850MB/123pngs = roughly 6.9MB per .png.... those would have to be some massive png's lol Smiley


-Pickle

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #28 - Posted 2013-01-17 23:29:55 »

That really does make sense... since 850MB/123pngs = roughly 6.9MB per .png.... those would have to be some massive png's lol Smiley


-Pickle

It's not the size of the .png, it's the size of the image in RAM.
This means that any .png compression is lost.

Offline GabrielBailey74
« Reply #29 - Posted 2013-01-17 23:30:50 »

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

Pages: [1] 2 3
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (38 views)
2014-04-15 18:08:23

BurntPizza (34 views)
2014-04-15 03:46:01

UprightPath (50 views)
2014-04-14 17:39:50

UprightPath (32 views)
2014-04-14 17:35:47

Porlus (49 views)
2014-04-14 15:48:38

tom_mai78101 (71 views)
2014-04-10 04:04:31

BurntPizza (130 views)
2014-04-08 23:06:04

tom_mai78101 (230 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!