I created an app which i recently refactored to use ViewModel and Fragments. At some time i run in to a problem: every time i tapped on a Edittext (or a subclass of it like EditTextInputLayout) my app crashes with an out of memory exeption. I don't even need to start typing to cause this error. The error occures everywhere, no matter if the EditText is part of a fragment, a simple activity or a Dialog.
05-23 12:44:55.712 13497-13497/com.ignates.myconomy E/AndroidRuntime: Error reporting crash
java.lang.OutOfMemoryError: Failed to allocate a 75738220 byte allocation with 16769200 free bytes and 54MB until OOM
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)
at java.lang.StringBuffer.append(StringBuffer.java:278)
at java.io.StringWriter.write(StringWriter.java:123)
at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358)
at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303)
at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625)
at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658)
at java.io.PrintWriter.append(PrintWriter.java:691)
at java.io.PrintWriter.append(PrintWriter.java:31)
at java.lang.Throwable.printStackTrace(Throwable.java:324)
at java.lang.Throwable.printStackTrace(Throwable.java:300)
at android.util.Log.getStackTraceString(Log.java:343)
at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:67)
at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:51)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:93)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
What i did:
- I used the Android Profiler trying to get a clue of what is going on. But since im not experienced in profiling i just coud determine that it allocates a lot of String, char[] and StringBuilder instances.
- I commented out any extension function on TextView or EditText
- Made sure ViewModels are implemented correctly and no instances of fragments are kept in memory
What i know
- At some point my every Edittext worked well, but i cant determine the actual point in time
- I am not using any BitMaps or ImageViews in my app
- The memory level rises from around 64mb to 256mb in about 5sec and then the app crashes. Also a lot of UncaughtExeptions appear in the callstack. I checked everything and don't know what causes these. Images of profiler: Profiler callstack, Profiler memory.
Example fragment
class SearchFragment : BaseFragment<SearchViewModel>(), SearchFragmentInterface, OnUserSelectedCallback {
val searchAdapter: SearchUserAdapter = SearchUserAdapter(this)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_search, container, false)
override fun createViewModel(): SearchViewModel = ViewModelProviders.of(activity,
ViewModelFactory { return@ViewModelFactory SearchViewModel(activity.application, activityViewModel, this) }).get(SearchViewModel::class.java)
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listener.setActionBar(search_toolbar) //Sets toolbar in activity
search_recycler.layoutManager = LinearLayoutManager(activity)
search_recycler.adapter = searchAdapter
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
search_input.isCursorVisible = false
/*search_input.onTextChange {
if (it != null && it.isNotEmpty()) mViewModel.queryUsers(it)
}*/
mViewModel.queryResults.observe(this, Observer {
it?.let { searchAdapter.setResults(it) }
})
}
override fun onUserSelected(user: UserNamePair) = mViewModel.onUserSelected(user)
override fun onError(e: Exception?, message: String) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
companion object {
@JvmStatic
fun newInstance() = SearchFragment()
}
I don't have much experience in these issues. Any help is appreciated.