简体   繁体   English

当 object 添加到 firebase - Android Studio 时,应用程序崩溃与 GC

[英]App crash with GC When an object added to firebase - Android Studio

Hello I have an Android app您好,我有一个 Android 应用程序

when an update ( Insert new data in firebase or update an attribute ) is done in firebase the app will crash for 15-30 sec.当在 firebase 中完成更新(在 firebase 中插入新数据或更新属性)时,应用程序将崩溃 15-30 秒。

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.当我检查日志时,它表明当时 GC 正在工作,并且需要花费大量时间来完成他的工作,因此该应用程序崩溃了很多时间,并且在电话上,您无法执行任何操作,直到 GC 在那里完成工作,我的数据库中没有太多数据,我的应用程序也没有那么大。

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:这是我如何从 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?有什么建议可以解决这个问题吗?

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.这是预期的行为,因为一次读取 1000 多个节点可以被视为大量数据,这很可能不适合 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.这意味着在Orders/City下还存在其他节点,这意味着要读取更多数据。 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.除此之外,最好根据您的需要通过调用Query#limitToFirst(int limit)Query#limitToLast(int limit)来限制您读取的数据量。 Besides that, it's also recommended to implement pagination .除此之外,还建议实现pagination

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 防止 android 可调用时应用程序崩溃 firebase function 抛出异常 - Prevent android app crash when callable firebase function throws exception Firebase 分析在运行 android 应用程序时崩溃 - Firebase analytics crash while running android app 当我从 Firebase Android Studio 读取或写入数据时,我的签名 APK 崩溃 - My Signed APK Crash when i read or write data from Firebase Android Studio Android 工作室 - 将 firebase 项目更改为 android 应用程序 - Android studio - Change firebase project for an android app 如何让 Android 和 iOS 的 Flutter App 崩溃(测试 Firebase Crashlytics)? - How to crash Flutter App for Android and iOS (to test Firebase Crashlytics)? 当 firebase 规则设置为 true 时,为什么我的应用程序会崩溃? - Why does my app crash when firebase rules are set to true? 为什么在 flutter 应用程序上访问 firebase 时,Windows Android Studio Emulator 会突然变慢? - Why would the Windows Android Studio Emulator suddenly become slow when accessing firebase on a flutter app? Kotlin 协程在 Android 应用程序中未完成时崩溃 - Crash when Kotlin Coroutine Not Complete in Android App 使用 Firebase 在应用程序(Android Studio)中上传个人资料图片 - Uploading profile picture in app (Android Studio) using Firebase Android 发送一个信号通知时应用程序崩溃 - Android App Crash when One Signal Notification sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM