i_me_mine
i_me_mine

Reputation: 1433

Android: Converting ArrayList to GSON runs out of memory

Hoping someone out there can explain this to me. I keep running out of memory when trying to store an ArrayList in my SharedPreferences.

public void setCurrentAlarmList(ArrayList<CurrentAlarm> currentAlarms) {
     Log.e("SETTING LIST", "!!!");
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
     SharedPreferences.Editor editor = prefs.edit();
     Gson gson = new Gson();
     String json = gson.toJson(currentAlarms);
     editor.putString(Constants.ALARM_LIST, json);
     editor.apply();
}

This is whats inside CurrentAlarm

public class CurrentAlarm {
    private ArrayList<AlarmManager> alarmManager;
    private String name;

    public CurrentAlarm() {}

    public ArrayList<AlarmManager> getAlarmManager() {
        return alarmManager;
    }

    public void setAlarmManager(ArrayList<AlarmManager> alarmManager) {
        this.alarmManager = alarmManager;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Then finally this is how I'm creating the Alarm Manager

AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.DAY_OF_WEEK, getDayOfTheWeek());
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
alarmsList.add(alarmMgr);

It's all pretty basic stuff. The last thing in the logcat is my "SETTING LIST" message, then it starts spitting out this:

Background partial concurrent mark sweep GC freed 18442(592KB) AllocSpace objects, 5(260KB) LOS objects, 0% free, 127MB/128MB, paused 56.464ms total 628.636ms

Followed inevitably by a crash because it runs out of memory.

FINAL LOG CAT AFTER CRASH

12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.Gson.toJson(Gson.java:593)
12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.Gson.toJson(Gson.java:572)
12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.Gson.toJson(Gson.java:527)
12-27 23:35:57.973 6204-6204/? E/art:     at com.google.gson.Gson.toJson(Gson.java:507)
12-27 23:35:57.973 6204-6204/? E/art:     at com.xxx.timeclock.controllers.SettingsController.setCurrentAlarmList(SettingsController.java:254)
12-27 23:35:57.973 6204-6204/? E/art:     at com.xxx.timeclock.controllers.SettingsController$2.onClick(SettingsController.java:239)

CODE AT LINE 254

String json = gson.toJson(currentAlarms);

I don't see why converting an array of alarm managers to a json string would cause this to happen. Whats going on here and how can I prevent this from happening? Thanks!

Upvotes: 0

Views: 483

Answers (1)

shaktiman_droid
shaktiman_droid

Reputation: 2633

AlarmManager is a system level class. Putting an array of AlarmManager in each CurrentAlarm and then putting them in sharedPrefs sounds like a really bad idea.

Just set the long time only in the sharedPrefs to retrieve later. Why need to create all alarms before hand ? Just an example without knowing your entire app/idea, run a Service once a day or something, check the time from shared pref and then create Alarm at that time.

Upvotes: 2

Related Questions