Unity RAM Optimization: Memory Profiler & Flyweight Pattern

Поділитися
Вставка
  • Опубліковано 6 січ 2025

КОМЕНТАРІ •

  • @PotatoManager420
    @PotatoManager420 Місяць тому

    Thank you, very useful to learn!

  • @TheCookingPixel
    @TheCookingPixel Місяць тому

    Thank you

  • @akaitoai2409
    @akaitoai2409 Місяць тому

    In my opinion despite the optimization it provides, it is also a major time saver.

    • @freedomcoding
      @freedomcoding  Місяць тому +1

      It depends how you implement the pattern. Using scriptable objects definitely saves time, but some other ways of implementing the pattern can make it unecessary complex.

    • @akaitoai2409
      @akaitoai2409 Місяць тому +1

      @@freedomcoding the approach you showed was a time saver.

  • @LotnyLotnik
    @LotnyLotnik Місяць тому +1

    Ok, but your example saves 16bits per object BITS, not kilobytes. (8bits for pointer, and 8 bits for value) To have any significant on any memory you would need to have 100 000 (HUNDRET THOUSAND) of such object and just then you would save just 200kB. Unity cannot run so many game objects at once, regardless if you use flyweight or not. If you saved more then that, then you cheated in your example. Also the "code" example uses strings which would never be stored in a game object like you shown. Yes those would weight a lot and save you memory, but this is just bad programming converted to a named "pattern" for views, this is just common sense tbh

    • @freedomcoding
      @freedomcoding  Місяць тому

      Hi, maybe I accidentally said kilobytes but I meant bytes. That should be obvious from the memory profiler window I showed. Regarding the pointers and values, they don't take up 8 bits, but rather 8 bytes - www.scaler.com/topics/size-of-a-pointer-in-c/ cplusplus.com/forum/general/200287/
      I can certainly say I didn't cheat the example. You can try it on your own and if you get different results, I would like to see them.
      Yes the example I shown at start was a bad programming practice. That is why I was able to demonstrate the flyweight on it. If I already had a perfect example with only good programming practices, there would probably be no need to apply the pattern.
      For some people this may not be common sense. I certainly would not think strings take up so much. Before I thought the sprites etc. will take up much more ram.
      If you have a better example using which I could have demonstrated the flyweight pattern, I would love to hear about it. Thank you for you opinion on the video.