0

I am making an app for android, in which at some point i need to add a lot of levels. To be efficient, after the player leaves a level, i delete it and add a new level, depending on some factors. The problem is, as I advance through the levels, the game gets a lot of lag. I looked on memory with device monitor and I saw that the memory used keeps going up, even if I use .clear to delete previous levels. I will put the code in and I tested it only with the player, without any other entities:

void manageLevels(){
    while(inc < sf && inc < minLevel){
    blocks.get(0).clear();
    inc++;
}

while(sf < numberLevels && sf < maxLevel + 2){
    blocks.add(new ArrayList<blockClass>());
    sf++;
    nrLv[sf] = blocks.size() - 1;
    float BlockX = -1500 * scaleX, BlockY = 250 * scaleY - (sf - 1) * levelSize;
    platforms[sf] = BlockY + 100 * scaleY;
    setTexture();
    for(int i = 0 ; i < numberOfBlocks ; i++){
        blocks.get(nrLv[sf]).add(new blockClass(BlockX, BlockY, typeT, scaleX, scaleY));
        BlockX += 100 * scaleX;
    }
}

}

Milos Fec
  • 828
  • 6
  • 11
Never Mind
  • 185
  • 1
  • 14
  • 2
    The memory increases because not all objects are released to the GC. Maybe this line `blocks.get(0).clear();` should not be 0 but `inc` or some other variable? – morpheus05 Dec 22 '15 at 09:44
  • Don't you want to remove the object from the list? After blocks.get(0).clear(); call blocks.remove(0); Anyway, we don't know what is in your blockClass. – Milos Fec Dec 22 '15 at 09:47
  • 3
    Btw, it is good to use java coding style: UpperCamelCase for classes and lowerCamelCase for variables (blockClass -> BlockClass, BlockX -> blockX) – Milos Fec Dec 22 '15 at 09:47
  • 2
    There's a good answer here on how to profile your memory usage, which will show which objects your holding onto casuing the "memory leak" http://stackoverflow.com/questions/24547555/how-to-analyze-memory-using-android-studio – Zain Dec 22 '15 at 09:51

0 Answers0