stringstream << operator consuming a lot of memory inside update(dt) method

I have an update method that is like an infinite loop because is run every frame. Inside this method I get a float and I have to convert it into a string, and on every frame set the score to the label.

I’m using the << operator that allows me to copy the float to an stringstream and then, i use the str() function to get the string value of the stream.

In the header file I declared scoreString, actualScore and scoreLabel.

update(dt){
    //more code

    actualScore += combo;
    scoreString.str("");
    scoreString << actualScore;
    scoreLabel->setString(scoreString.str());
    scoreString.clear();

    //more code
}

actualScore is the float that I want to convert to a string. For that purpuse, I use the scoreString object that is a stringstream. To recycle this stringstream I use the str(“”) function that set the value to nothing, and with this I don’t have to create the object every time that the loop is run.

This code it’s runned in iOS and android devices. I’m getting out of memory only in the android devices(affects to the performance of the apps, lag).

Here is my logCat output, but I think that it wouldn’t show nothing new.

01-12 15:35:25.271: D/dalvikvm(13948): GC_FOR_ALLOC freed 297K, 6% free 9580K/10160K, paused 20ms, total 20ms
01-12 15:35:25.321: D/dalvikvm(13948): GC_FOR_ALLOC freed 297K, 6% free 9580K/10160K, paused 20ms, total 20ms
01-12 15:35:25.371: D/dalvikvm(13948): GC_FOR_ALLOC freed 297K, 6% free 9580K/10160K, paused 21ms, total 21ms
01-12 15:35:25.421: D/dalvikvm(13948): GC_FOR_ALLOC freed 297K, 6% free 9580K/10160K, paused 19ms, total 19ms
01-12 15:35:25.472: D/dalvikvm(13948): GC_FOR_ALLOC freed 297K, 6% free 9580K/10160K, paused 18ms, total 18ms
01-12 15:35:25.522: D/dalvikvm(13948): GC_FOR_ALLOC freed 297K, 6% free 9580K/10160K, paused 14ms, total 14ms
01-12 15:35:25.572: D/dalvikvm(13948): GC_FOR_ALLOC freed 298K, 6% free 9581K/10160K, paused 15ms, total 16ms
...

PS. I also post this question on stackoverflow a few days ago, here is the link: http://stackoverflow.com/questions/21075930/stringstream-operator-consuming-a-lot-of-memory-inside-a-loop

Finally I found that the problem was for using CCLabelTTF that is slow and waste a lot of memory. I started using CCLabelBMFont that is faster.

You can see how at http://www.cocos2d-x.org/wiki/Text_Labels