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