1

I am building an app that shows user's FB friends' location on a map with their profile pic as the map marker icon.

After downloading friends' pics, I stored them on ArrayList on a global application object and then show them on the map. I've realized that I am spending a lot of heap memory for doing that.

I guess the right way to do it will be by storing them on the external storage, but I am not sure how to do it. Or maybe use SQLite database.

So my question is, what is the most efficient way to do that process? keep in mind that when a user's friend connect to the app, the app should show him on the map in real time.

Alon Levanon
  • 691
  • 5
  • 18

1 Answers1

0

Using ArrayList to store images does not scale very well, a better approach would be to store them in a database using Blob as described here:

How to save images into Database

Another way of doing this if you're not too comfortable with object persistance, is saving your images to filesystem and simply storing in a database table the pathnames to the images, so you can retrieve them when needed.

Community
  • 1
  • 1
  • Welcome to stack overflow, rock an, and thanks for your answer. For future reference, it's usually best to post the solution inside your stack overflow answer rather than just a link. That way, the answer will still be useful if the link dies one day. Thanks! – StackExchange What The Heck Jan 08 '14 at 22:54
  • Thank you for replying. What is the memory usage by storing them on HashMap? Isn't that similar to store them on an ArrayList? I am looking for a way to hold and retrieve those images without using lots of heap memory. – Alon Levanon Jan 09 '14 at 09:21
  • HashMap has a higher memory overhead than ArrayList, although the difference in performance is probably negligible for what you are trying to accomplish here, so use whichever suits you best. See here: [link](http://stackoverflow.com/questions/1526596/memory-overhead-of-java-hashmap-compared-to-arraylist) If you are only holding you data temporarily, I guess that approach would work but do bear in mind that heavy memory usage might get your process killed by the OS. – Ricardo Solano Jan 09 '14 at 10:44
  • Ok, let's say I've stored all images on a HashMap, where should I store the HashMap? At the moment, I am storing it on a global object (in an Application class) which I am using for keeping all my data accessible from any activity desired. Is it ok to keep big objects like that or may I need to store it on external storage? – Alon Levanon Jan 09 '14 at 11:59
  • You don't store a HashMap per se, you persist each Object contained in it and then re-instantiate from the database when needed. Another way of doing this if you're not too comfortable with object persistance, is saving your images to filesystem and simply storing in a database the pathnames to the images, so you can retrieve them when needed. Your current approach only works for temporarily doing something with your images, but variables are volatile meaning that if the os needs memory it will kill your app and you'll loose your data. Does this answer your question? – Ricardo Solano Jan 09 '14 at 13:02
  • Yes it is, thanks a lot. One last question, what is the most common way for apps doing that process (showing the user a lot of small images that should be downloaded)? – Alon Levanon Jan 09 '14 at 14:34
  • You're welcome. As for your last question I'm not sure, maybe post it as a new more detailed question? – Ricardo Solano Jan 09 '14 at 15:42