0

histogram image from eclipse-MatI am using Memory analyzer tool to detect posssible memory leak in my application and below is the image of heap profile which is saying i have 5 object of IntroActivity below is my code for introactivity i am not able to find from where this objects are created multiple times

public class IntroActivity extends AppCompatActivity {
[![enter image description here][1]][1]
    private ViewPager viewPager;
    private MyViewPagerAdapter myViewPagerAdapter=new MyViewPagerAdapter();
    private LinearLayout dotsLayout;
    private TextView[] dots;
    private int[] layouts;
    private Context context;
    private Button btnSkip, btnNext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Checking for first time launch - before calling setContentView()

        context=IntroActivity.this;
        // Making notification bar transparent
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }

        setContentView(R.layout.intro_layout);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
        btnSkip = (Button) findViewById(R.id.btn_skip);
        btnNext = (Button) findViewById(R.id.btn_next);


        // layouts of all welcome sliders
        // add few more layouts if you want
        layouts = new int[]{
                R.layout.intro_first_screen,
                R.layout.intro_second_screen,
                R.layout.intro_third_screen,
                R.layout.intro_fourth_screen,
                R.layout.intro_fifth_screen,
                R.layout.intro_sixth_screen,
                R.layout.intro_seventh_screen,
                R.layout.intro_eighth_screen};

        // adding bottom dots
        addBottomDots(0);

        // making notification bar transparent
        changeStatusBarColor();


        viewPager.setAdapter(myViewPagerAdapter);
        viewPager.addOnPageChangeListener(viewPagerPageChangeListener);

        btnSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchHomeScreen();
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // checking for last page
                // if last page home screen will be launched
                int current = getItem(+1);
                if (current < layouts.length) {
                    // move to next screen
                    viewPager.setCurrentItem(current);
                } else {
                    launchHomeScreen();
                }
            }
        });
    }

    private void addBottomDots(int currentPage) {
        dots = new TextView[layouts.length];

        int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
        int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

        dotsLayout.removeAllViews();
        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(this);
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(colorsInactive[currentPage]);
            dotsLayout.addView(dots[i]);
        }

        if (dots.length > 0)
            dots[currentPage].setTextColor(colorsActive[currentPage]);
    }

    private int getItem(int i) {
        return viewPager.getCurrentItem() + i;
    }

    private void launchHomeScreen() {
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    //  viewpager change listener
    ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            addBottomDots(position);

            // changing the next button text 'NEXT' / 'GOT IT'
            if (position == layouts.length - 1) {
                // last page. make button text to GOT IT
                btnNext.setText(getString(R.string.start));
                btnSkip.setVisibility(View.GONE);
            } else {
                // still pages are left
                btnNext.setText(context.getResources().getString(R.string.next));
                btnSkip.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };

    /**
     * Making notification bar transparent
     */
    private void changeStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * View pager adapter
     */
    public class MyViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;

        public MyViewPagerAdapter() {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(layouts[position], container, false);
            container.addView(view);

            return view;
        }

        @Override
        public int getCount() {
            return layouts.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


    }

can anyone please help in find possible leak any help is appriciated

Hardik Mehta
  • 867
  • 1
  • 12
  • 20

1 Answers1

0

No, you did not have 5 objects of IntroActivity.
Heap profile says there are one instance of IntroActivity and 4 instances that is inner class of IntroActivity.
Inner classes are below including anonymous class.

btnNext.setOnClickListener(new View.OnClickListener() {
    ...
}

btnNext.setOnClickListener(new View.OnClickListener() {
    ...
}

ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
    ...
}

public class MyViewPagerAdapter extends PagerAdapter {
    ...
}

Use LeakCanary to detect memory leaks.
https://github.com/square/leakcanary

nshmura
  • 5,940
  • 3
  • 27
  • 46
  • thank you so much can u please give suggestion regarding whether it is is good or bad to have such inner class..@nshmura – Hardik Mehta Aug 03 '16 at 04:42
  • as whenever this activity starts only for first time memory increases from 30 to 60-70 mb @nshmura – Hardik Mehta Aug 03 '16 at 04:54
  • The lifetime of instances of your inner class are same as your activity's lifetime. So I think there is no problem! Some times the inner class occurs memory leaks. see: http://stackoverflow.com/questions/33339583/android-inner-classes-memory-leak-and-leak-by-context – nshmura Aug 03 '16 at 04:56
  • > increases from 30 to 60-70 mb / I thinks this is normal situation, If you display large image. Try android:largeHeap https://developer.android.com/guide/topics/manifest/application-element.html – nshmura Aug 03 '16 at 04:59
  • exactly i m loding image directly..@nshmura – Hardik Mehta Aug 03 '16 at 05:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118973/discussion-between-hardik-mehta-and-nshmura). – Hardik Mehta Aug 03 '16 at 05:58
  • @HardikMehta Sorry, currently I have no time.. I can join 4h - 6h after – nshmura Aug 03 '16 at 06:26