垃圾收集正在减慢我的应用程序运行速度?

时间:2017-07-26 16:39:40

标签: java android multithreading performance crash

这是我正在运行的片段,它用于每次图像移动时用点运行if语句。除此之外还有一个自动计时器,其中图像根据我设置的时间移动。应用程序运行良好,直到我添加点功能。它仍在工作但速度非常慢,并且可能会缩短2/3分钟的运行时间。我可以更改或删除此代码的部分内容,还是可以在外部内存上运行应用程序? mayeb这会加快一点。我试图将drawables设置为局部变量,但仍然没有变化。还有什么我可以做的。

    public class HomeFragment extends Fragment {

    ViewPager viewPager;
    ViewPagerAdapter adapter;
    LinearLayout sliderDotsPanel;
    private int dotsCount;
    private ImageView[] dots;
    Drawable active_dots;
    Drawable nonactive_dots;


    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        sliderDotsPanel = (LinearLayout) view.findViewById(R.id.SliderDots);
        active_dots = getDrawable(getActivity().getApplicationContext(), R.drawable.active_dot);
        nonactive_dots = getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot);




        adapter = new ViewPagerAdapter(this.getActivity());
        viewPager.setAdapter(adapter);

        dotsCount = adapter.getCount();
        dots = new ImageView[dotsCount];

        for (int i = 0; i < dotsCount; i++) {

            dots[i] = new ImageView(getActivity());
            dots[i].setImageDrawable(nonactive_dots);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            params.setMargins(8, 0, 8, 0);
            sliderDotsPanel.addView(dots[i], params);


        }

        dots[0].setImageDrawable(active_dots);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                for (int i = 0; i < dotsCount; i++) {
                    dots[i].setImageDrawable(nonactive_dots);
                }

                dots[position].setImageDrawable(active_dots);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new myTimerTask(), 4000, 2000);
        return view;


    }


    public class myTimerTask extends TimerTask {

        @Override
        public void run() {

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {


                    while (true) {

                        if (viewPager.getCurrentItem() == 0) {
                            viewPager.setCurrentItem(1);
                        } else if (viewPager.getCurrentItem() == 1) {
                            viewPager.setCurrentItem(2);
                        } else if (viewPager.getCurrentItem() == 2) {
                            viewPager.setCurrentItem(1);
                        } else if (viewPager.getCurrentItem() == 1) {
                            viewPager.setCurrentItem(0);

                        }

                    }

                }


            });
        }
    }
}

下面的LogCat

07-26 14:19:44.486 2873-2873/com.example.aids.a09application W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
07-26 14:19:44.565 2873-2873/com.example.aids.a09application I/Choreographer: Skipped 117 frames!  The application may be doing too much work on its main thread.
07-26 14:19:46.622 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 787(89KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 198MB/198MB, paused 18.992ms total 102.229ms
07-26 14:19:46.952 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 997(40KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 198MB/202MB, paused 15.431ms total 173.177ms
07-26 14:19:47.181 2873-2878/com.example.aids.a09application I/art: Do partial code cache collection, code=30KB, data=27KB
07-26 14:19:47.194 2873-2878/com.example.aids.a09application I/art: After code cache collection, code=21KB, data=23KB
07-26 14:19:47.194 2873-2878/com.example.aids.a09application I/art: Increasing code cache capacity to 128KB
07-26 14:19:49.800 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 37941(1634KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 12.622ms total 296.630ms
07-26 14:19:52.394 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 36700(1551KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 13.130ms total 169.524ms
07-26 14:19:52.639 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 54270(3MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 200MB/204MB, paused 18.500ms total 234.230ms
07-26 14:19:54.910 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 66332(3MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 14.535ms total 197.841ms
07-26 14:19:57.531 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 75191(4MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 14.242ms total 205.903ms
07-26 14:19:59.749 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 35759(1500KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 12.808ms total 147.647ms
07-26 14:20:00.920 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 12875(540KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 204MB/205MB, paused 13.586ms total 113.208ms
07-26 14:20:01.340 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 95614(6MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 198MB/202MB, paused 16.117ms total 152.367ms
07-26 14:20:01.403 2873-2879/com.example.aids.a09application I/art: Thread[3,tid=2879,WaitingInMainSignalCatcherLoop,Thread*=0x9de8de00,peer=0x12c2a8b0,"Signal Catcher"]: reacting to signal 3
07-26 14:20:01.882 2873-2879/com.example.aids.a09application I/art: Wrote stack traces to '/data/anr/traces.txt'
07-26 14:20:05.687 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 45945(2MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 47.723ms total 976.583ms
07-26 14:20:09.410 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 33212(1393KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 18.694ms total 163.982ms
07-26 14:20:10.980 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 93472(5MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 199MB/203MB, paused 17.361ms total 219.162ms
07-26 14:20:13.670 2873-2880/com.example.aids.a09application W/art: Suspending all threads took: 10.526ms
07-26 14:20:13.771 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 52852(2MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 201MB/205MB, paused 15.208ms total 196.607ms
07-26 14:20:17.211 2873-2880/com.example.aids.a09application W/art: Suspending all threads took: 44.452ms
07-26 14:20:17.421 2873-2884/com.example.aids.a09application I/art: Background sticky concurrent mark sweep GC freed 35062(1470KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 203MB/205MB, paused 15.334ms total 273.230ms
07-26 14:20:19.082 2873-2884/com.example.aids.a09application I/art: Background partial concurrent mark sweep GC freed 89559(5MB) AllocSpace objects, 0(0B) LOS objects, 1% free, 199MB/203MB, paused 21.820ms total 251.707ms

1 个答案:

答案 0 :(得分:0)

删除while(true){就在循环之前。现在工作正常