3

I have a method in my program which I create a new fragment and start that fragment in it. But we have several Out Of Memory crashes in our app, So I decided to look at the memory profiler of the android studio and this is the result of opening several fragments with this piece of code: memory usage after opening several fragments

public void openFragment(MyCustomFragment fragment, boolean isDialog,
                         FragmentAnimationType animationType) {
    mStackCountAfterExternalIntent++;
    FragmentManager childFragmentManager = mFragmentAdapter.getItem(mActiveTab)
            .getChildFragmentManager();
    try {
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
            if (animationType != null) {
                fragmentTransaction.setCustomAnimations(animationType.enterAnimation, 0,
                        0, animationType.exitAnimation);
            }
            fragmentTransaction.replace(R.id.child_fragment_root, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commitAllowingStateLoss();
            if (childFragmentManager.getBackStackEntryCount() > 0) {
                FragmentManager fragmentManager = mFragmentAdapter.getItem(mActiveTab)
                        .getChildFragmentManager();
                MyCustomFragment fragmentToHide = (MyCustomFragment) fragmentManager.
                        getFragments().get(fragmentManager.getBackStackEntryCount() - 1);
            }
        System.gc();
        fragment.onVisible(true);
    } catch (Exception e) {
        App.getInstance().restartApp(mLastIntent, false);
    }
}

I think this memory usage is unusual, Why the memory usage never goes down after opening a new fragment? Is this a result of memory leak? Do you have any suggestion for more investigating?

Update1:

MyCustomFragment is a fragment which extends from Android DialogFragment and also is a base for another fragments in my project, all of my fragments extends from it.

Update2:

I changed my fragments to extends from Fragment instead of MyCustomFragment and problem does not solved.

Note1:

what is mFragmentAdapter?: We have a bottom menu in project and use ViewPager and FragmentPagerAdapter for handling it and its tabs, this variable used for that.

Hamidreza Sahraei
  • 457
  • 1
  • 5
  • 13
  • 2
    May be we need to look into `MyCustomFragment` – Faizan Mubasher Mar 27 '18 at 09:09
  • 4
    [System.gc() isn't good practice](https://stackoverflow.com/questions/2414105/why-is-it-bad-practice-to-call-system-gc) – 2Dee Mar 27 '18 at 09:12
  • Are you storing any fields in MyCustomFragment which have a context, for example any Views? they could be preventing MyCustomFragment from being GC'd – Peter Mar 27 '18 at 09:18
  • @FaizanMubasher It is just a customized and general fragment that extends from DialogFragment which another fragments in my project extend from it.Actually all of my fragments in my app extend from it. Need it? – Hamidreza Sahraei Mar 27 '18 at 09:26
  • What is the purpose of that (looks pretty bad) `if (childFragmentManager.getBackStackEntryCount() > 0) { FragmentManager fragmentManager = mFragmentAdapter.getItem(mActiveTab) .getChildFragmentManager(); MyCustomFragment fragmentToHide = (MyCustomFragment) fragmentManager. getFragments().get(fragmentManager.getBackStackEntryCount() - 1); } System.gc(); fragment.onVisible(true);` ? – Eselfar Mar 27 '18 at 09:26
  • 1
    @2Dee Yes, it was just for test, I will remove that line, Thank u for ur attention :) – Hamidreza Sahraei Mar 27 '18 at 09:27
  • @Peter Yes, there are some Fields with Type of "View", Actually they are some custom views, they cause this problem? – Hamidreza Sahraei Mar 27 '18 at 09:29
  • try to instantiate standard fragments and see if this happens. If it doesn't, post your custom fragments, custom views, everything you have – payloc91 Mar 27 '18 at 09:31
  • Yeah it seems that you have either messed up the backstack or more likely the fragment is holding a reference of himself (directly or via another object) and can't be garbage collected – Eselfar Mar 27 '18 at 09:32
  • I am really interested in seeing your implementation of mFragmentAdapter. – pablo432 Mar 27 '18 at 09:36
  • @HamidrezaSahraei it's a possibility and it's easy to check - just remove all the fields from your fragment and access everything with getView().findViewById() directly and see if that solves your leak. All views have Context which makes it a possibility for a leak. There are some additional common pitfalls here http://blog.nimbledroid.com/2016/05/23/memory-leaks.html – Peter Mar 27 '18 at 09:37
  • 1
    @MarkoPacak I tried your suggestion, but nothing changed, I have updated the question. – Hamidreza Sahraei Mar 27 '18 at 09:53
  • @HamidrezaSahraei I kind of ignored messed up backstack thing. If your implementation carelessly returns new fragment with every call to FragmentAdater#getItem, that could explain memory leaks. Also... why are you extending DialogFragment? It does not look for me like a good mix, I mean using DialogFragments + FragmentAdapter... – pablo432 Mar 27 '18 at 10:16
  • @Eselfar No it doesn't have any reference to itself, but would you explain about how backstack might be messed up? – Hamidreza Sahraei Mar 27 '18 at 10:19
  • @pablo432 Fragment adapter is for handling bottom menu which keep standard fragments in itself, MyCustomFragment is a dialog fragment for showing fragment as dialog in tablets and normal in phones. – Hamidreza Sahraei Mar 27 '18 at 10:21
  • What I meant was: instantiate plain fragments with your method and see if the error occurs again – payloc91 Mar 27 '18 at 10:37
  • I thought you where doing strange thing with the Fragments in the backstack but it seems ok. Can you please post your FragmentAdapter? – Eselfar Mar 27 '18 at 10:38
  • Same as @Eselfar, I'd look the problem inside the adapter. – pablo432 Mar 27 '18 at 13:35
  • How do you pass fragments to `openFragment`? Post that code as well – payloc91 Mar 28 '18 at 08:50

0 Answers0