Search code examples
androidout-of-memoryandroid-sharedpreferences

Save about 30 String Data in SharedPreferences and it Show java.lang.OutOfMemoryError


I want to save about 30 String data in my android app, but sometimes in android phone with almost full memory, this error appear:

Fatal Exception: java.lang.OutOfMemoryError at com.android.internal.util.FastXmlSerializer.(FastXmlSerializer.java) at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:177) at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:596) at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:52) at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:511) at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:532) at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:52) at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:454)

Is this still good to save about 30 string in sharedpreference?

I have read from here to not create a new editor for each change, maybe I have to change for some String change that call new editor, is that have a big contribution to prevent java.lang.OutOfMemoryError or I must do other else?


Solution

  • You can not store large amount of data in SharedPrefrences. Depending upon your mobile device you can store 1~2MB or even less. If you need to store large amount of data, then it is safe to use sqlLite database. There are several simple sqlLite library like http://satyan.github.io/sugar/