Memory Leak Issues When Using Data Binding with RecyclerView on Android 14
I'm trying to debug I'm a bit lost with I'm experiencing a memory leak in my Android app when using data binding in a RecyclerView. Specifically, when I scroll through the list, the app's memory usage keeps increasing, and I'm seeing `OutOfMemoryError` after some time. I've implemented RecyclerView with ViewHolder pattern and am using Data Binding with an observable list. To manage the binding, I set up the ViewHolder like this: ```kotlin class MyViewHolder(private val binding: ItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) { fun bind(item: MyItem) { binding.item = item binding.executePendingBindings() } } ``` In the adapter, Iām using the following code: ```kotlin class MyAdapter(private val items: List<MyItem>) : RecyclerView.Adapter<MyViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val binding = ItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false) return MyViewHolder(binding) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { holder.bind(items[position]) } override fun getItemCount(): Int = items.size } ``` I have tried using `binding.clear()` in `onViewRecycled()` but it doesn't seem to help. I've also run LeakCanary, which indicates that the ViewHolder instances are being retained by the Data Binding library. I verified that I'm not holding any static references to the Context or Views. Iām targeting Android 14 and using Kotlin with the latest version of the Android Gradle Plugin. Is there something specific Iām missing in managing the lifecycle of the binding objects, or is this a known scenario with data binding and RecyclerView? What are the best practices to avoid memory leaks in this scenario? This issue appeared after updating to Kotlin latest.