{"id":63,"date":"2024-12-02T21:29:37","date_gmt":"2024-12-02T08:29:37","guid":{"rendered":"https:\/\/systemreset.io\/journal\/?p=63"},"modified":"2024-12-02T21:29:39","modified_gmt":"2024-12-02T08:29:39","slug":"object-pooling-in-dart","status":"publish","type":"post","link":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/","title":{"rendered":"Object pooling in dart"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Object pooling in dart improves the memory performance of your Flutter app or game.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a large number of objects (such as bullets or enemies) are created over time in a Flutter game, dart has to do a lot of work allocating and then garbage collecting that memory. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The garbage collector (GC) does not run continuously, so memory usage can increase dramatically between sweeps, resulting in high spikes of memory usage. <a href=\"https:\/\/nimafarzin-pr.medium.com\/garbage-collection-in-dart-and-its-implications-in-flutter-aef9de51bfc4\">Read more about dart&#8217;s garbage collection.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Object pooling can help prevent fragmentation and reduce those peaks by re-using objects from a pool instead of creating and destroying them every time. This is especially useful on lower-memory devices such as smart watches or cheaper smart phones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation of object pooling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a simple implementation of object pooling in dart. At it&#8217;s core, the pool is simply a list of the same type of object.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ An object that is pooled should mix the Pooled mixin\nmixin Pooled {\n  \/\/ Implement this to reset the object ready for re-use\n  void reset();\n}\n\n\/\/ Object pool implementation\nclass Pool&lt;T extends Pooled&gt; {\n  \/\/ A list that holds the pooled objects\n  final List&lt;T&gt; _pool = &lt;T&gt;[];\n  \/\/ The method called to create a new pooled object\n  final T Function() _creator;\n\n  \/\/ Constructor\n  Pool(this._creator);\n\n  \/\/ Get an object from the pool, or create one if pool is empty\n  T get() {\n    if (_pool.isEmpty) {\n      return _creator();\n    } else {\n      T obj = _pool.removeLast();\n      obj.reset();\n      return obj;\n    }\n  }\n\n  \/\/ Add an object (back) to the pool\n  void add(T obj) {\n    _pool.add(obj);\n  }\n\n  \/\/ Clear the pool and release all the pooled objects for GC \n  void clear() {\n    _pool.clear();\n  }\n}\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9893A5; font-style: italic\">\/\/ An object that is pooled should mix the Pooled mixin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">mixin<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Pooled<\/span><span style=\"color: #575279\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Implement this to reset the object ready for re-use<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">void<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">reset<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9893A5; font-style: italic\">\/\/ Object pool implementation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">class<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Pool<\/span><span style=\"color: #575279\">&lt;<\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #286983\">extends<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Pooled<\/span><span style=\"color: #575279\">&gt; {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ A list that holds the pooled objects<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">final<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">List<\/span><span style=\"color: #575279\">&lt;<\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #575279\">&gt; _pool <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #286983\">&lt;<\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #286983\">&gt;<\/span><span style=\"color: #575279\">[]<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ The method called to create a new pooled object<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">final<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Function<\/span><span style=\"color: #575279\">() _creator<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Constructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #56949F\">Pool<\/span><span style=\"color: #575279\">(<\/span><span style=\"color: #575279; font-style: italic\">this<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">_creator)<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Get an object from the pool, or create one if pool is empty<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">get<\/span><span style=\"color: #575279\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    <\/span><span style=\"color: #286983\">if<\/span><span style=\"color: #575279\"> (_pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">isEmpty) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">      <\/span><span style=\"color: #286983\">return<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">_creator<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    } <\/span><span style=\"color: #286983\">else<\/span><span style=\"color: #575279\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">      <\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #575279\"> obj <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> _pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">removeLast<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">      obj<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">reset<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">      <\/span><span style=\"color: #286983\">return<\/span><span style=\"color: #575279\"> obj<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Add an object (back) to the pool<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">void<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">add<\/span><span style=\"color: #575279\">(<\/span><span style=\"color: #56949F\">T<\/span><span style=\"color: #575279\"> obj) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    _pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">add<\/span><span style=\"color: #575279\">(obj)<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Clear the pool and release all the pooled objects for GC <\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">void<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">clear<\/span><span style=\"color: #575279\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    _pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">clear<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">}<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Dart<\/span><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">To use it, create a new pool for every different object type that you want to pool. Bullets are a good choice as they are often created and destroyed in large numbers.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ Mix the Pooled mixin with the class that should be pooled\nclass Bullet with Pooled {\n  \/\/ Create a static pool, passing in the constructor of the object\n  static Pool&lt;Bullet&gt; pool = Pool&lt;Bullet&gt;(Bullet.new);\n  \n  \/\/ These are some example properties on Bullet\n  double damage = 1.0;\n  double speed = 20.0;\n  \n  \/\/ Set the Bullet back to default values when re-used\n  @override\n  void reset() {\n    damage = 1.0;\n    speed = 20.0;\n  }\n}\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9893A5; font-style: italic\">\/\/ Mix the Pooled mixin with the class that should be pooled<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">class<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #286983\">with<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Pooled<\/span><span style=\"color: #575279\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Create a static pool, passing in the constructor of the object<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">static<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Pool<\/span><span style=\"color: #575279\">&lt;<\/span><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #575279\">&gt; pool <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Pool<\/span><span style=\"color: #575279\">&lt;<\/span><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #575279\">&gt;(<\/span><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #286983\">new<\/span><span style=\"color: #575279\">)<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ These are some example properties on Bullet<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #56949F\">double<\/span><span style=\"color: #575279\"> damage <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">1.0<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #56949F\">double<\/span><span style=\"color: #575279\"> speed <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">20.0<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #9893A5; font-style: italic\">\/\/ Set the Bullet back to default values when re-used<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">@override<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  <\/span><span style=\"color: #286983\">void<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">reset<\/span><span style=\"color: #575279\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    damage <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">1.0<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    speed <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">20.0<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">}<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Dart<\/span><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Then, whenever you need a new bullet get it from pool. Also remember to add it back to the pool once it is no longer needed.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ Instead of this: Bullet b = Bullet();\nBullet b = Bullet.pool.get();\n\n\/\/ Once the bullet is no longer required\nBullet.pool.add(b);\n\n\/\/ When the game is over, free any pooled objects\nBullet.pool.clear();\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9893A5; font-style: italic\">\/\/ Instead of this: Bullet b = Bullet();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #575279\"> b <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">get<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9893A5; font-style: italic\">\/\/ Once the bullet is no longer required<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">add<\/span><span style=\"color: #575279\">(b)<\/span><span style=\"color: #797593\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9893A5; font-style: italic\">\/\/ When the game is over, free any pooled objects<\/span><\/span>\n<span class=\"line\"><span style=\"color: #56949F\">Bullet<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">pool<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #D7827E\">clear<\/span><span style=\"color: #575279\">()<\/span><span style=\"color: #797593\">;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Dart<\/span><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the result &#8211; these are screengrabs of the memory profiler without and with object pooling enabled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without object pooling, as many as 1000 bullets can remain in memory before the garbage collector sweep removed them.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"647\" src=\"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-1024x647.png\" alt=\"Memory usage without object pooling\" class=\"wp-image-65\" srcset=\"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-1024x647.png 1024w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-300x190.png 300w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-768x486.png 768w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-1536x971.png 1536w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling.png 1558w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Memory usage without object pooling<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With object pooling enabled there are a maximum of 20 bullets in memory. Instead of creating new objects, recycled bullets are re-used from the pool. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"649\" src=\"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-with-pooling-1024x649.png\" alt=\"Memory usage with object pooling\" class=\"wp-image-66\" srcset=\"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-with-pooling-1024x649.png 1024w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-with-pooling-300x190.png 300w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-with-pooling-768x487.png 768w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-with-pooling-1536x974.png 1536w, https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-with-pooling.png 1558w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Memory usage with object pooling<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object pooling in dart improves the memory performance of your Flutter app or game. When a large number of objects (such as bullets or enemies) are created over time in a Flutter game, dart has to do a lot of work allocating and then garbage collecting that memory. The garbage collector (GC) does not run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,8,6],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-dart","category-game-development","category-performance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Object pooling in dart - system reset<\/title>\n<meta name=\"description\" content=\"Object pooling in dart reduces fragmentation and memory peaks by re-using objects from a pool instead of creating and destroying them.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Object pooling in dart - system reset\" \/>\n<meta property=\"og:description\" content=\"Object pooling in dart reduces fragmentation and memory peaks by re-using objects from a pool instead of creating and destroying them.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/\" \/>\n<meta property=\"og:site_name\" content=\"system reset\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-02T08:29:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-02T08:29:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1558\" \/>\n\t<meta property=\"og:image:height\" content=\"985\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"System Reset\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"System Reset\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/\"},\"author\":{\"name\":\"System Reset\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#\\\/schema\\\/person\\\/06a6e24e1a9461e4155cfc86c122dff5\"},\"headline\":\"Object pooling in dart\",\"datePublished\":\"2024-12-02T08:29:37+00:00\",\"dateModified\":\"2024-12-02T08:29:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/\"},\"wordCount\":282,\"publisher\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/bullet-mem-usage-no-pooling-1024x647.png\",\"articleSection\":[\"Dart\",\"Game development\",\"Performance\"],\"inLanguage\":\"en-NZ\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/\",\"url\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/\",\"name\":\"Object pooling in dart - system reset\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/bullet-mem-usage-no-pooling-1024x647.png\",\"datePublished\":\"2024-12-02T08:29:37+00:00\",\"dateModified\":\"2024-12-02T08:29:39+00:00\",\"description\":\"Object pooling in dart reduces fragmentation and memory peaks by re-using objects from a pool instead of creating and destroying them.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#breadcrumb\"},\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#primaryimage\",\"url\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/bullet-mem-usage-no-pooling.png\",\"contentUrl\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/bullet-mem-usage-no-pooling.png\",\"width\":1558,\"height\":985,\"caption\":\"Memory usage without pooling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/object-pooling-in-dart\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Object pooling in dart\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#website\",\"url\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/\",\"name\":\"system reset\",\"description\":\"dev journal\",\"publisher\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#organization\",\"name\":\"system reset\",\"url\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/system-reset-stacked-1024-1.png\",\"contentUrl\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/system-reset-stacked-1024-1.png\",\"width\":1024,\"height\":1024,\"caption\":\"system reset\"},\"image\":{\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/systemreset.io\\\/journal\\\/#\\\/schema\\\/person\\\/06a6e24e1a9461e4155cfc86c122dff5\",\"name\":\"System Reset\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/edbe9aa08d216b02d0781180e15a10381c672def2d9ec0eac19143bb28a9284a?s=96&d=blank&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/edbe9aa08d216b02d0781180e15a10381c672def2d9ec0eac19143bb28a9284a?s=96&d=blank&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/edbe9aa08d216b02d0781180e15a10381c672def2d9ec0eac19143bb28a9284a?s=96&d=blank&r=g\",\"caption\":\"System Reset\"},\"sameAs\":[\"https:\\\/\\\/systemreset.io\\\/journal\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Object pooling in dart - system reset","description":"Object pooling in dart reduces fragmentation and memory peaks by re-using objects from a pool instead of creating and destroying them.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/","og_locale":"en_US","og_type":"article","og_title":"Object pooling in dart - system reset","og_description":"Object pooling in dart reduces fragmentation and memory peaks by re-using objects from a pool instead of creating and destroying them.","og_url":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/","og_site_name":"system reset","article_published_time":"2024-12-02T08:29:37+00:00","article_modified_time":"2024-12-02T08:29:39+00:00","og_image":[{"width":1558,"height":985,"url":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling.png","type":"image\/png"}],"author":"System Reset","twitter_card":"summary_large_image","twitter_misc":{"Written by":"System Reset","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#article","isPartOf":{"@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/"},"author":{"name":"System Reset","@id":"https:\/\/systemreset.io\/journal\/#\/schema\/person\/06a6e24e1a9461e4155cfc86c122dff5"},"headline":"Object pooling in dart","datePublished":"2024-12-02T08:29:37+00:00","dateModified":"2024-12-02T08:29:39+00:00","mainEntityOfPage":{"@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/"},"wordCount":282,"publisher":{"@id":"https:\/\/systemreset.io\/journal\/#organization"},"image":{"@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#primaryimage"},"thumbnailUrl":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-1024x647.png","articleSection":["Dart","Game development","Performance"],"inLanguage":"en-NZ"},{"@type":"WebPage","@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/","url":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/","name":"Object pooling in dart - system reset","isPartOf":{"@id":"https:\/\/systemreset.io\/journal\/#website"},"primaryImageOfPage":{"@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#primaryimage"},"image":{"@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#primaryimage"},"thumbnailUrl":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling-1024x647.png","datePublished":"2024-12-02T08:29:37+00:00","dateModified":"2024-12-02T08:29:39+00:00","description":"Object pooling in dart reduces fragmentation and memory peaks by re-using objects from a pool instead of creating and destroying them.","breadcrumb":{"@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#breadcrumb"},"inLanguage":"en-NZ","potentialAction":[{"@type":"ReadAction","target":["https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/"]}]},{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#primaryimage","url":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling.png","contentUrl":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/12\/bullet-mem-usage-no-pooling.png","width":1558,"height":985,"caption":"Memory usage without pooling"},{"@type":"BreadcrumbList","@id":"https:\/\/systemreset.io\/journal\/object-pooling-in-dart\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/systemreset.io\/journal\/"},{"@type":"ListItem","position":2,"name":"Object pooling in dart"}]},{"@type":"WebSite","@id":"https:\/\/systemreset.io\/journal\/#website","url":"https:\/\/systemreset.io\/journal\/","name":"system reset","description":"dev journal","publisher":{"@id":"https:\/\/systemreset.io\/journal\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/systemreset.io\/journal\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-NZ"},{"@type":"Organization","@id":"https:\/\/systemreset.io\/journal\/#organization","name":"system reset","url":"https:\/\/systemreset.io\/journal\/","logo":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/systemreset.io\/journal\/#\/schema\/logo\/image\/","url":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/11\/system-reset-stacked-1024-1.png","contentUrl":"https:\/\/systemreset.io\/journal\/wp-content\/uploads\/2024\/11\/system-reset-stacked-1024-1.png","width":1024,"height":1024,"caption":"system reset"},"image":{"@id":"https:\/\/systemreset.io\/journal\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/systemreset.io\/journal\/#\/schema\/person\/06a6e24e1a9461e4155cfc86c122dff5","name":"System Reset","image":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/secure.gravatar.com\/avatar\/edbe9aa08d216b02d0781180e15a10381c672def2d9ec0eac19143bb28a9284a?s=96&d=blank&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/edbe9aa08d216b02d0781180e15a10381c672def2d9ec0eac19143bb28a9284a?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/edbe9aa08d216b02d0781180e15a10381c672def2d9ec0eac19143bb28a9284a?s=96&d=blank&r=g","caption":"System Reset"},"sameAs":["https:\/\/systemreset.io\/journal"]}]}},"_links":{"self":[{"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":2,"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/posts\/63\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systemreset.io\/journal\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}