IAmYourFaja
IAmYourFaja

Reputation: 56934

Dart: Memory management of DOM tree?

I am wondering if I need to be worried about - or even be sensitive to - memory management with my Dart app.

Say I have a web app with 2 "views"/"screens"/"pages". View #1 has:

View #2 has:

Now let's say that there are buttons on each view to allow the user to toggle back and forth between each view, over and over again: View 1 --> View 2 --> View 1 --> View 2, etc.

I am worried about constantly recreating/reinstantiating DOM elements over and over, and eventually tying up memory resources, if the user keeps toggling back and forth between the views. So I ask:

Upvotes: 2

Views: 602

Answers (2)

ringstaff
ringstaff

Reputation: 2325

Memory management should always be a concern, since you will always want your app to be fast and not tie up resources it shouldn't. Garbage collection is, however, automatically performed by the browser on any object which is unreachable (not referenced in the DOM). This does not mean you should ignore this topic altogether as it is still possible to create leaks. Here's a nice tutorial on memory leaks in javascript.

In your case, it looks like the fastest option would be to keep both views in a single HTML file, then control which is shown programmatically. This way the other view is waiting in memory and ready to display. A simple way to do this would be to change the display style on the views you want to show/hide. So your views could be enclosed in divs:

<div id="view1">
  //stuff in view #1 (visible)
</div>

<div id="view2" style="display:none">
  //stuff in view #2 (hidden initially)
</div>

And when you want to trigger between views:

querySelector('#view1').style.display = 'none';
querySelector('#view2').style.display = 'block';

This approach will require a larger memory footprint than loading each page individually. However, it would avoid having to instantiate every element each time a view loads, and it would avoid the garbage collector running every time you close a view.

Upvotes: 1

Syler
Syler

Reputation: 1171

Have you looked into benchmarking your test harness ? https://www.dartlang.org/articles/benchmarking/ Seems like you can extend the benchmark base class and write your own test code to benchmark against production VM.

Upvotes: 1

Related Questions