Jasper Heeren
Jasper Heeren

Reputation: 11

How can I recycle objects that are no longer needed before Garbage Collector comes into action?

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;
                }
            }
        }

Upvotes: 1

Views: 63

Answers (1)

Stefano Maglione
Stefano Maglione

Reputation: 4150

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.

Upvotes: 1

Related Questions