Is creating the next for the loop harmful to the garbage collection?

I have an update method that updates the level in the game every milliseconds. Inside this method, I have several loops that look like this:

for (int i = 0: i < 10; ++i){

}

When I speak:

int i = 0

Does this create a lot of garbage? And will there be an alternative?

thank

+4
source share
1 answer

When you write int i = 0, it does not create garbage.

A variable ihas a type intthat is a primitive type, not a type of object (or reference). The state is iheld entirely inside the stack frame of the attached method; those. not on the heap.

Does this create a lot of garbage?

No.

And will there be an alternative?

...

+7

Source: https://habr.com/ru/post/1529529/


All Articles