Out of Memory error using firebase

2020-04-21 09:29发布

问题:

I am getting an oom error when reading from firebase database on android. I'm using fragments for tabs, this is the code for one of the tabs..

public class EventsTab extends Fragment {

ListView listView;
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> descriptions = new ArrayList<String>();
ArrayList<String> dates = new ArrayList<String>();
ArrayList<String> urls = new ArrayList<String>();
int counter = 0;
float x1,x2;
float y1, y2;

static ProgressDialog dialog;
static AnimationDrawable frameAnimation;
static ProgressBar mProgressBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    titles = new ArrayList<String>();
    urls = new ArrayList<String>();
    dates = new ArrayList<String>();
    descriptions = new ArrayList<String>();

    View rootView = inflater.inflate(R.layout.activity_events_tab, container, false);
    return rootView;

}

@Override 
public void onActivityCreated(Bundle savedInstanceState) {  
    super.onActivityCreated(savedInstanceState); 

        mProgressBar = (ProgressBar) getActivity().findViewById(R.id.my_progress);
        mProgressBar.setBackgroundResource(R.drawable.ani_icon);

        frameAnimation = (AnimationDrawable) mProgressBar.getBackground();

        frameAnimation.start();


    Firebase ref = new Firebase("https://dazzling-inferno-7627.firebaseio.com/yourLocal/events/");
    ref.addValueEventListener(new ValueEventListener() {

        @Override
          public void onDataChange(DataSnapshot snapshot) {
            System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");


            for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                Event post = postSnapshot.getValue(Event.class);
                titles.add(post.getTitle());
                dates.add(post.getDate());
                descriptions.add(post.getDescription());

              }

                listView = (ListView) getActivity().findViewById(R.id.listView3);

               CustomList adapter = new  CustomList(getActivity(), titles, dates);

                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                        Intent intent = new Intent(getActivity(), ShowEvent.class);
                        intent.putExtra("title", titles.get(position));
                        intent.putExtra("date", dates.get(position));
                        intent.putExtra("description", descriptions.get(position));
                        startActivity(intent);  

                    }
                });
                frameAnimation.stop();
                mProgressBar.setVisibility(View.INVISIBLE);
          }
          @Override
          public void onCancelled(FirebaseError firebaseError) {
              System.out.println("The read failed: " + firebaseError.getMessage());
          }
      });



}

}

This is the Error...

10-27 11:46:36.444: E/AndroidRuntime(11090): FATAL EXCEPTION: main
10-27 11:46:36.444: E/AndroidRuntime(11090): java.lang.OutOfMemoryError
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:82)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.StringBuilder.<init>(StringBuilder.java:67)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.core.util.TextBuffer.contentsAsString(TextBuffer.java:350)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.core.json.ReaderBasedJsonParser.getValueAsString(ReaderBasedJsonParser.java:223)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:29)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.brianmccabe.yournews.EventsTab$Test$1.onDataChange(EventsTab.java:107)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.os.Handler.handleCallback(Handler.java:615)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.os.Looper.loop(Looper.java:137)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.app.ActivityThread.main(ActivityThread.java:4867)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.reflect.Method.invokeNative(Native Method)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.reflect.Method.invoke(Method.java:511)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at dalvik.system.NativeStart.main(Native Method)

回答1:

You are downloading more data from Firebase than will fit in memory. Try splitting up the data, using queries, and reducing the scope of the read operation.