I have an app in which I have more than 450 custom Java objects, and about 200 other variables of strings, int, etc. I know that these much variables use large RAM of the device, so I want to know the best way for storing and accessing them. Some variables are used in many activities.
Asked
Active
Viewed 93 times
1
-
It depends on many things. What they contains, they purpose, how often they are accessed, etc... – vincrichaud Mar 05 '19 at 15:40
2 Answers
1
I would definitely persist them to a a file since they are so many. Doing this you can load the classes in when you need them and not worry about memory
Saving custom class
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(this);
os.close();
fos.close();
Loading custom Class
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
SimpleClass simpleClass = (SimpleClass) is.readObject();
is.close();
fis.close();
Here´s another question with some cool implementations of this

Erik
- 5,039
- 10
- 63
- 119
0
you can use data base for storing variables in memory but it better to use Sharedprefrences
to store all variables :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putBoolean("key_name", true);
editor.commit();
and easilly get the variable :
pref.getString("key_name", null);

keivan shamlu
- 137
- 9
-
i use SharedPreferences to save score and progresses , but will it be ideal to save more than 600 variable in it? – Ismaeel Sherif Mar 05 '19 at 16:38
-
1Don't store this much amount of variables in sharedPrefs, every time you access sharedPrefs all of them will be loaded in memory. – Kashish Sharma Mar 05 '19 at 17:13