简体   繁体   English

从Firebase加载大型json时,Android应用返回java.lang.OutOfMemoryError

[英]Android app returns java.lang.OutOfMemoryError when loading large json from Firebase

I have a machine learning app that takes an array of weights from Firebase every time it updates. 我有一个机器学习应用程序,每次更新时都会从Firebase中获取一系列权重。 In the past this worked fine, but since I have begun implementing a neural network, that array size has jumped from 8,000 to 200,000. 在过去,这种方法效果很好,但是自从我开始实现神经网络以来,该数组的大小已从8,000跃升至200,000。 Because of this, when I pull the array as a json object, I get this error: 因此,当我将数组拉为json对象时,会出现以下错误:

java.lang.OutOfMemoryError
                                                                  at java.util.HashMap.makeTable(HashMap.java:569)
                                                                  at java.util.HashMap.doubleCapacity(HashMap.java:589)
                                                                  at java.util.HashMap.put(HashMap.java:419)
                                                                  at com.google.android.gms.internal.zzahj.getValue(Unknown Source)
                                                                  at com.google.android.gms.internal.zzahj.getValue(Unknown Source)
                                                                  at com.google.android.gms.internal.zzahj.getValue(Unknown Source)
                                                                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                  at myapp.DataSend$5.onDataChange(DataSend.java:223)
                                                                  at com.google.android.gms.internal.zzafp.zza(Unknown Source)
                                                                  at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
                                                                  at com.google.android.gms.internal.zzags$1.run(Unknown Source)
                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                  at dalvik.system.NativeStart.main(Native Method)

And the code in question (Datasend 223) is this: 有问题的代码(Datasend 223)是这样的:

public void checkoutListener(){
        final TextView message = (TextView) findViewById(R.id.messageDisplay);
        userValues.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userCheck = dataSnapshot.getValue(UserData.class);
            }

            @Override
            public void onCancelled(DatabaseError firebaseError) {

            }
        }

        );

    }

Is there any way I can prevent this, or is an array of 200,000 doubles just too much for the android heap to handle? 有什么办法可以防止这种情况发生,或者200,000的双精度数组对于android堆来说太过分了?

Try to make use of the flyweight design pattern here. 在这里尝试利用flyweight设计模式。 Best would be to store your weights in external memory and read in batches while applying the same to the network nodes. 最好将权重存储在外部存储器中,并在将其应用于网络节点时分批读取。 You might need to change the way the weights are being set to the nodes of the hidden layers in your neural network. 您可能需要更改对神经网络中隐藏层的节点设置权重的方式。

Flyweight design pattern 轻量化设计模式

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM