Search code examples
ember.jsmemory-leakscoffeescriptcomputed-observable

Will ember leak memory if I create a Model in a computed property?


I'm creating a "scrambler" that takes in a text array and an image array, then computes the cross product as tweets. The function that I'm worried about looks like this:

combinations: (->
  tweet_texts  = @get('tweet_texts')
  tweet_images = @get('tweet_images')

  # return empty array unless we have texts
  return Em.A([]) unless tweet_texts.length

  # handle the case when we don't have images
  unless tweet_images.length
    combinations  = tweet_texts.map (text) =>
      TwitterPost.create
        text : text
        newtwork_user : @get('account.twitter_handle')
    return Em.A(combinations)

  # handle texts and images
  combinations = tweet_images.map (image) =>
    tweet_texts.map (text) =>
      TwitterPost.create
        text  : text
        image : image
        network_user : @get('account.twitter_handle')
  return Em.A([].concat(combinations...))
).property('tweet_texts.@each','tweet_images.@each')

My worry is that I'm creating a lot of models and I don't really understand Ember's garbage collection.

So, am I at risk of creating a memory leak here?

Thanks!


Solution

  • Ember doesn't implement any sort of garbage collection. In a few instances involving the DOM it does remove it's references to items to make sure it's garbage collected.

    Ember doesn't create any sort of global reference to objects created using Foo.create(). So the moment there are no longer references to the object and your browser decides it is in the mood to collect, it will be collected.

    Making it a computed property is analogous to making it a property on any object. The moment the object is no longer referenced, it will be marked for collection, and the properties will follow.