Note that there are some explanatory texts on larger screens.

plurals
  1. PODealing with memory making Swing animations
    primarykey
    data
    text
    <p>It's my first time writting there but after reading and searching a lot about the subject I'm going to explain, I didn't found anything useful for me (I've checked a post of a guy explaining something similar about an iphone app, but without a final solution). Sorry for my english I'll try to do my best hehe :).</p> <p>Ok, I'm starting: I have java application that shows a set of animations at the same time on a JPanel. These animations are java objects from <strong>Animation</strong> (inheritates from JLabel), a class of my own <em>that stores an array of ImageIcon</em> (representing the frames of animation) and an integer pointer, showing actual frame to paint (index). That class has overriden the method setPaint() in that way:</p> <pre><code>@Override public void paintComponent(Graphics g) { g.drawImage(getImageAt(getCurrentIndex()).getImage(), getX2(), getY2(), getW(), getH(), null); //getImage at retrieves the frame at a "index" position from frame array. if (getCurrentIndex() &lt; getTotal() - 1) { incrementIndex();//increments frame by 1 (next frame) } else { resetIndex();//set index to 0 (frame 0) } } </code></pre> <p>On the other hand, we have <strong>Display</strong>, which inheritates from JPanel and is responsible for repainting the panel (it uses I javax.swing.Timer object and <strong>repaints every 50 ms</strong>).</p> <p>All the images from every Animation object have the same size 768x576 and png format with transparencies (thus I can paint many animations at a time).</p> <p>The problem is that when I load an animation it occupies too much memory because it creates I don't know why either how 200 MB of "int" objects apart of the 2MB that really occupies. When I load more animations the heap's space of the JVM rockets (up to <strong>1.7 GB</strong>... I didn't want to try anymore xD). For example, <strong>6 animations may be 405 frames</strong> (90 in average). I've collected this data with JProfiler, but I can't post the capture so this what I get: Memory usage of my app by JProfiler:</p> <ul> <li><p>java.awtRectangle 3351 KB</p></li> <li><p>sun.java2d.SunGraphics2D 16.510 KB (I think this is the size of all the animations and sounds logical).</p></li> <li><p>java.lang.String 669KB</p></li> <li><p>int[] 1337 MB ...</p></li> </ul> <hr> <p>In display, the loading images process is in this way:</p> <pre><code>add(new Animation (loadImageSet(Constants.DIR_CINTA_EJE,"all"), Constants.A_CINTA_EJE, 75,-200,768,576));//1 </code></pre> <p>Where DIR_CINTA_EJE is the directory where all the frames of that animation are and the value "all" means that I want to load all the frames.</p> <p>There is a way to avoid that memory used grows linearly with the number of animations? Why those ​​"int" values occupies so much? Are they the pixels of each image?</p> <p>And yes, I have Increased my heap space up to 2GB and it's working, but at a high cost. Thank you very much for all the help you can provide. I love this forum, really.</p> <p>Regards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload