简体   繁体   English

Android:将ArrayList转换为GSON会耗尽内存

[英]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 . 尝试将ArrayList存储在我的SharedPreferences ,我一直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 这是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: logcat中的最后一件事是我的“ SETTING LIST”消息,然后它开始吐出:

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 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. 我不明白为什么将警报管理器数组转换为json字符串会导致这种情况发生。 Whats going on here and how can I prevent this from happening? 这是怎么回事,如何防止这种情况发生? Thanks! 谢谢!

AlarmManager is a system level class. AlarmManager是系统级别的类。 Putting an array of AlarmManager in each CurrentAlarm and then putting them in sharedPrefs sounds like a really bad idea. 在每个CurrentAlarm放置一个AlarmManager数组,然后将它们放置在sharedPrefs中,听起来是一个非常糟糕的主意。

Just set the long time only in the sharedPrefs to retrieve later. 只需在sharedPrefs中设置long时间,以供以后检索。 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. 这只是一个示例,您可能不知道您的整个应用程序/想法,每天运行一次Service或类似的操作,从共享偏好检查时间,然后在该时间创建警报。

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

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