如何解决我的应用程序的内存消耗

问题描述

我已经构建了一个电子商务android应用程序。但是我的应用程序的内存消耗很高。它在3小时内消耗500 MB。有时,它在内存不足的手机上无法正常工作。当我测试时,它的内存不足,应用程序将崩溃。我是编程的新手,我不知道编程技术。请帮我解决这个问题。这是我的代码

public class HomeFragment extends Fragment {
    private View homeFragmentVeiw;
    private static ArrayList<Products> list,newLists,brandList;
    private ArrayList<Category> listx;
    private static ArrayList<Merchants> codeList=new ArrayList<>();
    private LinearLayout store,createStore,inviteFreinds,inviteSellers,barnd_layout;
    private static RecyclerView recyclerView1,catRec,new_products_rec,brandRec;
    private RelativeLayout mainLayout;
    private ImageSlider imageSlider;
    private TextView txtCreateStore;
    private TextView moreProductsTxt;
    private TextView newProductstxt;
    private ImageView imgCreateStore;
    private ProgressBar progressBar;
    private static String userName;
    private static String userNickName;
    private String package_name;
    private static ArrayList<Merchants> totalInvitation;
    private static Object invited_by;
    private static String txtinvitation=null,noOfInvitation;
    private linearlayoutmanager layoutManager1;
    private static DatabaseReference userCodeRef,ref =  FirebaseDatabase.getInstance().getReference();
    //private Picasso picasso;
    private List<SlideModel> slideModels;
    private  linearlayoutmanager layoutManager;
   // private CategoryAdapter categoryAdapter;
    private Intent intent;
    private String subject1,subject2;
    private static int check=0,checko=0;
    private static Dialog dialog;
    private static ArrayAdapter<String> adapter;
    private static ArrayList<String> listb;
    private NoConnections noConnections;
    private SellerListner sellerListner;
    private PagerListner pagerListner;
    private CategoryListner categoryListner;
    private UserListner userListner;
    private ProductListner productListner;
    private SaveInvitationListner saveInvitationListner;
    private static Query query;
    private static int checkP = 0;
    private RequestManager glide;
    private CardView scan;
    private RequestManager glidep;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,final Bundle savedInstanceState) {
        homeFragmentVeiw = inflater.inflate(R.layout.fragment_home,container,false);
        glidep = Glide.with(this);
        glide = Glide.with(getContext());

        ref =  FirebaseDatabase.getInstance().getReference();
        codeList=new ArrayList<>();
        if (getContext()!=null) {
            //package name
            package_name = getContext().getPackageName();

            dialog = new Dialog(getContext());

            scan = homeFragmentVeiw.findViewById(R.id.Scan_QR);
            scan.setVisibility(View.GONE);
            FirebaseDatabase.getInstance().getReference().child("Admin_QR_Trade").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        if (dataSnapshot.getValue().toString().equals("on")){
                            scan.setVisibility(View.VISIBLE);
                        }else{
                            scan.setVisibility(View.GONE);
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
            scan.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(),QRCodeRunner.class);
                    startActivity(intent);
                }
            });
            barnd_layout = homeFragmentVeiw.findViewById(R.id.brand_list);
            barnd_layout.setVisibility(View.GONE);
            brandRec = homeFragmentVeiw.findViewById(R.id.brand_rec);
            brandRec.setLayoutManager(new linearlayoutmanager(getContext(),linearlayoutmanager.HORIZONTAL,false));
            brandRec.setHasFixedSize(false);
            showBrand();

            store = homeFragmentVeiw.findViewById(R.id.store);
            inviteSellers = homeFragmentVeiw.findViewById(R.id.user_invite_sellers);
            inviteFreinds = homeFragmentVeiw.findViewById(R.id.invite_friends);
            createStore = homeFragmentVeiw.findViewById(R.id.create_store);
            new_products_rec = homeFragmentVeiw.findViewById(R.id.new_product_rec);
            RecyclerView.LayoutManager LayoutMgr = new
                    StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            new_products_rec.setLayoutManager(LayoutMgr);

            moreProductsTxt = homeFragmentVeiw.findViewById(R.id.more_products_title);
            imgCreateStore = homeFragmentVeiw.findViewById(R.id.img_create_store);
            txtCreateStore = homeFragmentVeiw.findViewById(R.id.txt_create_store);
            mainLayout = homeFragmentVeiw.findViewById(R.id.main_layout);
            progressBar = homeFragmentVeiw.findViewById(R.id.home_fragment_progress);
            recyclerView1 = homeFragmentVeiw.findViewById(R.id.product_rec);
            newProductstxt = homeFragmentVeiw.findViewById(R.id._title);
            RecyclerView.LayoutManager mLayoutManager = new
                    StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            layoutManager1 = new GridLayoutManager(getContext(),2);
            recyclerView1.setLayoutManager(mLayoutManager);
//
            //checking the user has seller account
            if (getContext()!=null) {
                sellerListner = new SellerListner(txtCreateStore,getContext(),imgCreateStore,glide);
                ref.child("Sellers").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).addValueEventListener(sellerListner);
            }

            imageSlider = homeFragmentVeiw.findViewById(R.id.banners);
            slideModels = new ArrayList<>();
            //the view pager
            pagerListner = new PagerListner(slideModels,imageSlider);
            ref.child("banners").addValueEventListener(pagerListner);
            //for category images

            catRec = homeFragmentVeiw.findViewById(R.id.cat_rec);

            catRec.setHasFixedSize(true);
            layoutManager = new linearlayoutmanager(getContext());
            catRec.setLayoutManager(layoutManager);
            //retriving the category
            categoryListner = new CategoryListner(listx,progressBar,moreProductsTxt,mainLayout,newProductstxt);
            ref.child("Category").addValueEventListener(categoryListner);

            store.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkconnection();
                    intent = new Intent(getContext(),StoreActivity.class);
                    startActivity(intent);
                }
            });
            //checking initation and showing the dialogue
            userListner = new UserListner(getContext(),progressBar);
            ref.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).addValueEventListener(userListner);
            inviteFreinds.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (ref.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()) == null) {
                        checkconnection();
                    } else {
                        //checking the user has account or not
                        if (userNickName != null) {
                            intent = new Intent(Intent.ACTION_SEND);
                            intent.setType("text/plain");
                            subject1 = getString(R.string.Hello_friends_my_name_is) + " " + userName + " " + getString(R.string.inviting_friends) + "\n" + getString(R.string.you_can_get_it) + "\n " + getString(R.string.write_this_code) + " " + userNickName + " " + getString(R.string.for_supporting) + "\n" + getString(R.string.thankyou) + "\n" + "https://play.google.com/store/apps/details?id=" + package_name;
                            intent.putExtra(Intent.EXTRA_TEXT,subject1);
                            startActivity(Intent.createChooser(intent,"Share via"));
                        } else {
                            Toast.makeText(getContext(),getString(R.string.creat_account),Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
            inviteSellers.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if ( ref.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()) == null) {
                        checkconnection();
                    } else {
                        //checking the user has account or not
                        if (userNickName != null) {
                            intent = new Intent(Intent.ACTION_SEND);
                            intent.setType("text/plain");
                            subject2 = getString(R.string.Hello_friends_my_name_is) + " " + userName + " " + getString(R.string.inviting_users) + "\n" + getString(R.string.you_can_get_it) + "\n" + getString(R.string.thankyou) + "\n" + "https://play.google.com/store/apps/details?id=" + package_name;
                            intent.putExtra(Intent.EXTRA_TEXT,subject2);
                            startActivity(Intent.createChooser(intent,Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
            createStore.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkconnection();
                    intent = new Intent(getContext(),SellerActivity.class);
                    startActivity(intent);
                }
            });
//            //geting all product
            productListner = new ProductListner(getContext(),glidep);
            query = FirebaseDatabase.getInstance().getReference().child("Products").orderByChild("published_on_date");
            query.addValueEventListener(productListner);
            //saveing invitations to database
            saveInvitationListner = new SaveInvitationListner();
            ref.child("Users").addValueEventListener(saveInvitationListner);

            checkconnection();

            if (ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            } else {
                ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION},20);
            }
        }

        return homeFragmentVeiw;
    }



    private void showBrand() {
        FirebaseDatabase.getInstance().getReference().child("Brand").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                     brandList = new ArrayList<>();
                     for (DataSnapshot ds : dataSnapshot.getChildren()){
                         if (ds.child("availability").getValue().equals("enabled")){
                             brandList.add(ds.getValue(Products.class));
                             barnd_layout.setVisibility(View.VISIBLE);
                         }
                     }
                     BrandAdapter brandAdapter = new BrandAdapter(brandList,getContext());
                     brandRec.setAdapter(brandAdapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }


    public void checkconnection() {
        noConnections = new NoConnections(getContext());
        noConnections.checkconnection();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        check = 0;checko = 0;
        sellerListner=null;
        pagerListner=null;
        categoryListner=null;
        userListner=null;
        productListner=null;
        saveInvitationListner=null;
        glide=null;
        dialog = null;
        glidep = null;
    }
}

页面正在占用大量内存。请帮助我解决问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)