56

I am storing String into shared preferences, but I wasn't able to find out maximal length of string I can store. Do you know this value?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Waypoint
  • 17,283
  • 39
  • 116
  • 170
  • If it is really huge, like over 10 kilobyte, you might want to save it some other way, maybe a file on disk. – Fred Jul 11 '14 at 14:51

3 Answers3

93

By Romain Guy From the Question Asked Here,

Whatever the maximum length of a Java string is. So something like Integer.MAX_VALUE chars.

I suppose while SharedPreference is an XML file stored with One Tag if you store only One Pair/Object. So there is no limit to write String in that if you think like you are writing in a file..(Theoretically)

But Actually what happens is that, while you are assigning value to SharedPreference using put/get function at that time you Reading/Writting value in String Object..so the limit becomes to store value at one time equal to Size limit of String Object of Java.

So while writting the code: Limit of SharedPreference String Size = Java String Object Size Limit(Practically)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
25

Shared preference is stored in /data/data/[package_name]/shared_prefs/[app name].xml, I think there's no limit as per android architecture.

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
20

When I trying to save the max length string into the SharedPreferences in my device, it throw a Memory Exception when SharedPreferences data exceed 1.42 MB.

So if you have more than 1.42 MB data size to save its better to save use SQLite database.

According to developer.android.com:

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mani
  • 3,394
  • 3
  • 30
  • 36