sharedpreferences android 内存 vmHeap

Posted

技术标签:

【中文标题】sharedpreferences android 内存 vmHeap【英文标题】:sharedpreferences android memory vmHeap 【发布时间】:2016-07-06 04:31:53 【问题描述】:

我有一个活动设置,我在 sharedPreferences 中为一个键设置了一个值。 如果我将此键设置为 2-3-4 次 App 崩溃。

03-19 12:07:00.701 12542-12542/giordano.piercarmine.crossfitpersonalrecords I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 6% free, 54MB/58MB, paused 10ms total 20ms 03-19 12:07:00.701 12542-12542/giordano.piercarmine.crossfitpersonalrecords I/art: Forcing collection of SoftReferences for 10MB allocation 03-19 12:07:00.701 12542-12542/giordano.piercarmine.crossfitpersonalrecords I/art: Starting a blocking GC Alloc 03-19 12:07:00.711 12542-12542/giordano.piercarmine.crossfitpersonalrecords I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 6% free, 54MB/58MB, paused 0 total 10ms 03-19 12:07:00.711 12542-12542/giordano.piercarmine.crossfitpersonalrecords W/art: Throwing OutOfMemoryError "Failed to allocate a 10840012 byte allocation with 4194304 free bytes and 9MB until OOM" 03-19 12:07:00.721 12542-12542/giordano.piercarmine.crossfitpersonalrecords D/skia: --- decoder->decode returned false 03-19 12:07:00.721 12542-12542/giordano.piercarmine.crossfitpersonalrecords D/androidRuntime: Shutting down VM 03-19 12:07:00.721 12542-12542/giordano.piercarmine.crossfitpersonalrecords E/AndroidRuntime: FATAL EXCEPTION: main Process: giordano.piercarmine.crossfitpersonalrecords, PID: 12542 java.lang.OutOfMemoryError: Failed to allocate a 10840012 byte allocation with 4194304 free bytes and 9MB until OOM

这是我的 SharedPreferences 类:

` 公共类 SessionManager // LogCat 标记 //私有静态字符串 TAG = SessionManager.class.getSimpleName();

// Shared Preferences
SharedPreferences pref;

Editor editor;
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Shared preferences file name
private static final String PREF_NAME = "AndroidHiveLogin";

private static final String KEY_IS_LOGGEDIN = "isLoggedIn";

private static final String TYPE_OF_WEIGHT = "Kg";

public SessionManager(Context context) 
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();


public void setLogin(boolean isLoggedIn) 

    editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn);

    // commit changes
    editor.commit();

    //Log.d(TAG, "User login session modified!");


public boolean isLoggedIn()
    return pref.getBoolean(KEY_IS_LOGGEDIN, false);


public void setTypeOfWeight(String typeOfWeight)
    editor.putString(TYPE_OF_WEIGHT, typeOfWeight);
    editor.apply();



public String getTypeOfWeight()
    return pref.getString(TYPE_OF_WEIGHT, "Kg");

` 在所有活动中,我用 new 创建了这个类的实例。这是正确的方法吗?

【问题讨论】:

【参考方案1】:

您似乎节省了大约 10 兆字节的值。 SharedPreferences 不用于存储数据。它旨在存储小的键值对,例如设置、用户名、ID、选项等。

您可以改用 SQLLite。

【讨论】:

谢谢伙计。我发现了错误。当我在活动设置中单击保存按钮时,我通过意图和 startActivity(intent) 切换到另一个活动 (mainActivity)。如果我多次单击按钮保存,我会创建此活动的多个实例,并且每次内存增加 10mb

以上是关于sharedpreferences android 内存 vmHeap的主要内容,如果未能解决你的问题,请参考以下文章

Android-SharedPreferences

android开发之路11(用SharedPreferences存储数据)

Android 工具类 SharedPreferences 封装

android----SharedPreference

Android - 具有可序列化对象的 SharedPreferences

Android中SharedPreference多进程数据共享出错