locked
Memory allocation - which one is better? RRS feed

  • Question

  • which one is better in term of performance e.g. CPU, memory?

    1. object is created in the loop

    e.g.

    for()
    {
        object p = new object;
    }


    2. object is created prior and is reused in the loop

    e.g.

    object p = new object;
    
    for()
    {
       // p is reused
    }

    For #1, I understand that it's good in term of GC because the object is died very quickly - Gen0, but I am not sure in term of performance impact of memory allocation.

    For #2, this is good in term of memory allocation? but the object may be promoted into Gen1/2 which is not good.

    So I am not sure which option is better?

    Saturday, July 5, 2014 5:36 PM

Answers

  • "For #2, this is good in term of memory allocation? but the object may be promoted into Gen1/2 which is not good."

    That's kind of true but repeatedly creating the object isn't great either. Anyway, the exact impact of a object being moved to Gen2 depends on a lot of factors.

    "So I am not sure which option is better?"

    What matters most is that the code is correct. There are cases where the object cannot be reused and cases where the object must be reused, in all such cases the question about performance is irrelevant since you don't have a choice.

    In those cases where both choices are valid personally I'd stick with choice 1 unless performance is a concern and measurements (or common sense, a loop that does millions of iterations is a good candidate for optimization while a loop that does only 10 iterations is unlikely to be) indicate that choice 2 is more efficient.

    The reason I'd choose 1 is not related to performance, it's just that I prefer to define local variables in the innermost scope they are used. That is, if variable p is not used after the for loop then variable p should be defined inside the for loop. And that implies that the object creation will happen inside the for loop.

    Saturday, July 5, 2014 6:58 PM
  • Hello,

    These two options are not interchangeable so how can you even compare them? the former means you need to create an instance with each iteration whereas the latter means you don't.

    You don't design an application based on how things perform but based on requirements, meaning, you have a need to do either one of these things and when the performance of the application degrades only then you start to worry about it.

    I'm with Mike though I'd go for the first option and I also like immutable, small objects so it fits. :)


    Regards, Eyal Shilony

    Saturday, July 5, 2014 7:36 PM

All replies

  • "For #2, this is good in term of memory allocation? but the object may be promoted into Gen1/2 which is not good."

    That's kind of true but repeatedly creating the object isn't great either. Anyway, the exact impact of a object being moved to Gen2 depends on a lot of factors.

    "So I am not sure which option is better?"

    What matters most is that the code is correct. There are cases where the object cannot be reused and cases where the object must be reused, in all such cases the question about performance is irrelevant since you don't have a choice.

    In those cases where both choices are valid personally I'd stick with choice 1 unless performance is a concern and measurements (or common sense, a loop that does millions of iterations is a good candidate for optimization while a loop that does only 10 iterations is unlikely to be) indicate that choice 2 is more efficient.

    The reason I'd choose 1 is not related to performance, it's just that I prefer to define local variables in the innermost scope they are used. That is, if variable p is not used after the for loop then variable p should be defined inside the for loop. And that implies that the object creation will happen inside the for loop.

    Saturday, July 5, 2014 6:58 PM
  • Hello,

    These two options are not interchangeable so how can you even compare them? the former means you need to create an instance with each iteration whereas the latter means you don't.

    You don't design an application based on how things perform but based on requirements, meaning, you have a need to do either one of these things and when the performance of the application degrades only then you start to worry about it.

    I'm with Mike though I'd go for the first option and I also like immutable, small objects so it fits. :)


    Regards, Eyal Shilony

    Saturday, July 5, 2014 7:36 PM