Skip to content
Advertisement

App crash with GC When an object added to firebase – Android Studio

Hello I have an Android app

when an update ( Insert new data in firebase or update an attribute ) is done in firebase the app will crash for 15-30 sec.

when I checked the log, it show that’s in that time the GC is working and it takes a lot of time to do his work so the app crashed a lot of time and on the phone, you can’t do anything till GC finishes there work, I don’t have so much data in my database also my application is not that bigger.

You can see the log here :

I/lhindeliveryma: Background concurrent copying GC freed 325485(8953KB) AllocSpace objects, 0(0B) LOS objects, 36% free, 10MB/16MB, paused 218us total 136.046ms
W/Looper: Slow Looper main: Long Msg: seq=1424 plan=02:06:39.877  late=2ms wall=2060ms running=1848ms runnable=39ms io=29ms h=android.os.Handler c=com.google.firebase.database.core.view.EventRaiser$1
I/lhindeliveryma: Background concurrent copying GC freed 373886(10MB) AllocSpace objects, 0(0B) LOS objects, 35% free, 11MB/17MB, paused 259us total 155.904ms
I/lhindeliveryma: Background concurrent copying GC freed 501873(13MB) AllocSpace objects, 0(0B) LOS objects, 33% free, 11MB/17MB, paused 394us total 247.891ms
I/lhindeliveryma: Background young concurrent copying GC freed 323701(8831KB) AllocSpace objects, 0(0B) LOS objects, 31% free, 12MB/17MB, paused 537us total 116.240ms
I/lhindeliveryma: Background concurrent copying GC freed 567601(15MB) AllocSpace objects, 0(0B) LOS objects, 32% free, 12MB/18MB, paused 430us total 288.562ms
I/lhindeliveryma: Background young concurrent copying GC freed 351662(9586KB) AllocSpace objects, 0(0B) LOS objects, 32% free, 12MB/18MB, paused 528us total 125.947ms
W/Looper: Slow Looper main: Long Msg: seq=1425 plan=02:06:40.037  late=1902ms wall=1811ms running=1692ms runnable=2ms h=android.os.Handler c=com.google.firebase.database.core.view.EventRaiser$1
I/Choreographer: Skipped 180 frames!  The application may be doing too much work on its main thread.

here’s what log is saying

and this is my code how i receive data from firebase :

        FirebaseAuth mAuth;
        mAuth = FirebaseAuth.getInstance();

        FirebaseUser user = mAuth.getCurrentUser();


        firebaseDatabase = FirebaseDatabase.getInstance();


        EndLoop:
        databaseReference = firebaseDatabase.getReference().child("Orders").child(City);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {




                You = new ArrayList<>();
                Now = new ArrayList<>();


                postAd = new OrderAD();

                boolean stop = false;

                for (DataSnapshot postsnap : dataSnapshot.getChildren()) {
                    if(!stop)
                    for (DataSnapshot postsnap1 : postsnap.getChildren()) {
                        if(!stop)
                        for (DataSnapshot postsnap2 : postsnap1.getChildren()) {
                            if (!stop) {
                                Order post = postsnap2.getValue(Order.class);

                                if (!post.getStatus().equals("تم التوصيل") && !post.getStatus().equals("تم الغاء الطلب")) {




                                    if (post.getDeliveryUID().equals(user.getUid()))
                                        You.add(post);
                                    else {
                                        if (post.getDeliveryUID().equals("")) {
                                            Now.add(post);

                                        }

                                    }

                                    if (!Offers.contains(post.getKey())) {
                                        Offers.add(post.getKey());
                                        if (Done) {
                                            show_Notification(post);
                                            You.clear();
                                            Now.clear();
                                            stop = true;
                                            System.out.println("Hello");
                                            break;
                                        }
                                    }

                                }

                                break;


                            }
                        }

                    }
                }

                if(!Done){
                    Done = true;
                }



                Collections.sort(Now);
                Collections.sort(You);


                postAd = new OrderAD(Offers.this, Now);
                recyclerView.setAdapter(postAd);








            }

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

            }
        });

any suggestion to solve this?

Advertisement

Answer

The app will crash.

It’s the expected behavior since reading 1000+ nodes at once can be considered a lot of data, that most likely doesn’t fit into the memory. Besides that, I see that you’re looping through the children three times. This means that under Orders/City there are also other nodes present, this means even more data to read. In the Realtime Database, when you attach a listener to a particular location in the database, you read (download) that node along with all the children beneath it. In such cases, it’s best to denormalize the data. This means that you should create another node that should contain only the data you are interested in a flat list of nodes. Besides that, it’s best the limit the amount of data that you read by calling Query#limitToFirst(int limit) or Query#limitToLast(int limit) according to your needs. Besides that, it’s also recommended to implement pagination.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement