Which is more expensive for memory. "creating and deleting objects" or "reusing an object"?

I have an object that is needed through the game every 10 seconds. Should I continue to delete the object or continue to use the same object? Where is the object in the so-called "free time"?

since this is a mobile game, memory is a concern. so I just wanted to know which method would be fruitful.

"creating and deleting objects" ?

or

"reusing a object" ?

thank

+5
source share
4 answers

This greatly depends on the nature of the object and the memory usage of the rest of the program, but as a rule:

, . , . , , .

, , , , ( ).

+3

, (, , ), .

+1

?

, . ( std::string g++, ) . , ; , , () , .

, , , . , std::ios_base; std::ostringstream, . .

+1
source

When the object is no longer in use, the destructor is called to free memory. But if you then create a new object of the same type, the constructor is called to allocate new memory. This, of course, reduces performance (in practice, however, it also depends on how large the object is), so if you no longer need the object at runtime, you must reuse it by changing its contents.

0
source

All Articles