将ArrayList转换为GSON会耗尽内存

将ArrayList转换为GSON会耗尽内存

本文介绍了Android:将ArrayList转换为GSON会耗尽内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人在那里可以向我解释这一点.当尝试在我的 SharedPreferences 中存储 ArrayList 时,我一直用不完内存.

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();
}

这是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);

这都是非常基本的东西.logcat中的最后一件事是我的"SETTING LIST"消息,然后它开始吐出来:

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.

崩溃后的最终日志猫

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)

第254行的代码

String json = gson.toJson(currentAlarms);

我不明白为什么将警报管理器数组转换为json字符串会导致这种情况发生.这是怎么回事,如何防止这种情况发生?谢谢!

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!

推荐答案

AlarmManager 是系统级别的类.在每个 CurrentAlarm 中分别放置一个 AlarmManager 数组,然后将它们放置在sharedPrefs中,听起来真是个坏主意.

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.

只需在sharedPrefs中设置 long 时间,以便以后检索.为什么需要事先创建所有警报?只是一个示例,您可能不知道您的整个应用程序/想法,每天或一次运行一次 Service ,从共享的偏好检查时间,然后在那时创建Alarm.

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.

这篇关于Android:将ArrayList转换为GSON会耗尽内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:16