clojure-android / neko

The Clojure/Android Toolkit
Other
297 stars 36 forks source link

to-dimension leaks activities! #59

Open domokato opened 8 years ago

domokato commented 8 years ago

Providing a vector to :layout-height or :layout-width such as [10 :dp] leaks the Activity because eventually to-dimension is called with an Activity Context as a parameter ((.getContext wdg)), which then calls get-display-metrics if it gets a vector. get-display-metrics is memoized, so it keeps a reference to its parameter, the Activity, which then prevents the Activity from getting garbage collected after it finishes.

To reproduce:

  1. Create two activities with relative-layouts or linear-layouts in them
  2. Use vector dimensions in the layout-width and/or layout-height
  3. Go back and forth between the activities
  4. Use a memory profiler to view the memory usage, filter by the activity names Result: You should see multiple instances of each activity kept in memory Expected result: You should only see at most one instance of each activity

To fix, either:

  1. Use (.getApplicationContext (.getContext wdg)) instead in various parts in traits.clj.
  2. Stop memoizing get-display-metrics.

Note: For any users out there reading this, it remains safe to provide a vector to :text-size since it doesn't call to-dimension.

I'm using 3.2.0, but looking at the latest source the issue should still happen.