1

My teacher gave me the task to download a 2d game and improve the performance of the application. now I'm kinda stuck and I decided to download Jprofiler to check the memory the game uses etc. I saw that the app is using a ton of rectangles and that they only get deleted when the garbace collector comes around and does it's thing. My question is: is there a way to get rid of these rectangles before the GC comes around? Here's a picture of the Jprofiler scan.

Jprofiler Scan

The Sprite object has an ArrayList of Rectangles for the bounds.

tileX = (int)xMap/tileXSize;
    tileY = (int)yMap/tileYSize;
    accurateX = -(int)xMap%tileXSize;
    accurateY = -(int)yMap%tileYSize;
    for (int i=tileX; i<tileX+displayX+1 && i<sizeX; i++) {
        for (int j=tileY; j<tileY+displayY+1 && j<sizeY; j++) {
            Sprite s = spriteMap[j][i];
            if (s != null) {
                s.act();
                // Colisones con los objetos de al lado
                if (inMap(i+1,j) && spriteMap[j][i+1] != null) {
                    if (s.collidesWith(spriteMap[j][i+1], false)) {
                        s.collision(spriteMap[j][i+1]);
                        spriteMap[j][i+1].collision(s);
                    }
                }
                if (inMap(i,j+1) && spriteMap[j+1][i] != null) {
                    if (s.collidesWith(spriteMap[j+1][i], false)) {
                        s.collision(spriteMap[j+1][i]);
                        spriteMap[j+1][i].collision(s);
                    }
                }
                if (inMap(i+1,j+1) && spriteMap[j+1][i+1] != null) {
                    if (s.collidesWith(spriteMap[j+1][i+1], false)) {
                        s.collision(spriteMap[j+1][i+1]);
                        spriteMap[j+1][i+1].collision(s);
                    }
                }
                if (s.isToDelete()) {
                    spriteMap[j][i] = null;
                }
            }
        }
  • 1
    what actual performance problems are there? What are the rectangles doing, they *could* all be relevant to the program and must not be collected!? – luk2302 Nov 05 '18 at 15:17
  • 1
    Possible duplicate of [What is the best way to clean up an Object in Java?](https://stackoverflow.com/questions/1380640/what-is-the-best-way-to-clean-up-an-object-in-java) – Arnaud Nov 05 '18 at 15:19
  • Is it possible for you to provide the code that creates and uses rectangle objects ? – janardhan sharma Nov 05 '18 at 15:26
  • There aren't really any performance problems but the amount of Rectangles keep growing. the program starts with around 100k rectangles and in lets say 5 minutes the program has 3.5 million rectangles. I assume the program doesn't need all 3.5 million of them. and it uses 100mb of space when it reaches 3.5 million Rectangles. – Jasper Heeren Nov 05 '18 at 15:27
  • 2
    You have to define your idea of “performance”. The garbage collector does not run, because there’s plenty of unused memory left and there is no advantage of having unused memory. That’s a good strategy regarding CPU consumption. The garbage collector does not waste CPU cycles when it doesn’t run. When you want the application to consume less memory, you can set an upper limit, e.g. using the `-Xmx` option. Then, the garbage collector will run more frequently, reducing the memory consumption, but raising the CPU consumption. – Holger Nov 05 '18 at 15:40
  • 1
    @JasperHeeren 100 MB isn't a lot of memory unless you are running on a device. You can recycle the Rectangles using a pool, but it probably isn't worth spending much time on. 2 GB is worth about 1 hour of your time on a PC or server. 100 MB is worth about 3 mins. – Peter Lawrey Nov 05 '18 at 19:03
  • @PeterLawrey and Holger thanks for the information! – Jasper Heeren Nov 05 '18 at 20:40
  • 1
    As already said, allocation/GC is most probably not the problem. 100 MB is nothing I'd bother to change; forget memory, maybe the speed could be improved. The collision detection algorithm (which you din't show us) might be the thing to work on. – maaartinus Nov 06 '18 at 21:16

1 Answers1

1

You can improve the performance analysing the time complexity of your code. After the analysis phase you can try to reduce the complexity (when possible). You need to go from exponential time complexity to linear or logarithm.

Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96