Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.support.constraint.ConstraintLayout has leaked:

I'm using LeakCanary to detect memory leaks in an app. I successfully identified and fixed some leaks using it, but I am struggling find the root of this leak:

* android.support.constraint.ConstraintLayout has leaked:
* Toast$TN.mNextView
* ↳ LinearLayout.mContext
* ↳ HomeActivity.!(mDelegate)!
* ↳ AppCompatDelegateImplN.!(mActionBar)!
* ↳ ToolbarActionBar.!(mDecorToolbar)!
* ↳ ToolbarWidgetWrapper.!(mToolbar)!
* ↳ Toolbar.mParent
* ↳ ConstraintLayout
* Reference Key: 552b5bc5-409d-44c4-8412-87341237ae6d
* Device: samsung samsung SM-G960F starltexx
* Android Version: 8.0.0 API: 26 LeakCanary: 1.6.2 0ebc1fc
* Durations: watch=5769ms, gc=153ms, heap dump=933ms, analysis=5802ms

Is this leak caused by the Android SDK or app specific code?

like image 655
SebRut Avatar asked Nov 20 '18 14:11

SebRut


People also ask

What is a ConstraintLayout in Android?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.

How do I change ConstraintLayout to LinearLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

What is the latest version of constraint layout?

constraintlayout:constraintlayout:2.1. 0 is released. This is the final release for 2.1. 0.

What is constraint horizontal bias in Android?

Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis". The default bias is 0.5, meaning that the widget is centered in the available space.


1 Answers

The leak was caused by app specific code. One fragment called getActivity().setSupportActionBar() with a view contained in the fragments layout. When switching to another fragment the resources couldn't get gc'ed because the activity was still holding a reference to the no longer visible toolbar. I fixed this leak by calling getActivity().setSupportActionBar(null) in the fragments onDestroyView() method, which removes the reference to the toolbar.

like image 125
SebRut Avatar answered Sep 28 '22 23:09

SebRut