Should one use static variables or store data in database

Although my question arose from building an android application but i think its a general programming question in java.

I hava a set of three images which i want to load. Now loading the images takes time so there is a lag of around 2 seconds. What i am thinking is of maintaining a static variable array which will store the Bitmap of all the images.
This way when the user comes back to the screen i dont have to reload the Bitmap images but just take them from the Bitmap static array....

Although i am wondering if it can be stored in some other way say a db?

I want to know the pros and cons of storing a static variable and storing data in a database.
Is it a good idea to store the Bitmap somwhere else or store them in a static array ?
Can storing in static variable lead to bugs ? or is it a good programming practice ?

Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I want to know the pros and cons of storing a static variable and storing data in a database.

The main tradeoff is memory (RAM) vs speed.

If you store the images in a static variable they will continue to use RAM for as long as your app is running.  In runtime they will be immediately available for later use with no delay.

If you store the images in a local file or in a database, then there's no RAM used (they're stored on disk instead) but they'll need to be reloaded from the data store when you wish to show them.  That will be faster than downloading the image (which I assume is where you were loading it originally?) but still takes some time.  So this approach is slower, but uses less RAM.

So which is better?  It depends.  If the images are large or you have lots of them, you may not want to keep them in RAM.  Say you have 3 images and each is 1MB (of RAM once loaded).  If you only show one at a time, you could use store them in the database (or local file) and use a total of 1MB for showing them (as only one is loaded at a time).  If instead you keep all 3 in a static variable, then you need to use 3MB all of the time.

Since you say it's 3 images, it's likely that it's fine to keep them in a static variable.  But if it was 15, the answer is probably you shouldn't (unless you will show all 15 on screen at once, in which case you need to be able to handle that memory usage anyway).

Hope that helps,


P.S. Lots of general software problems are like this where you can trade space (memory usage) for speed.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
thanks for explanation.
so say i create a static array of 3 elements each containing a bitmap.  now i initialize it at a point with 3 bitmap.
if later on i need to remove them from ram will just setting the array = null will do or i need to specifically assign null to each element of the array.
Setting the array to null will clear it.

More specifically, the garbage collector will remove the bitmaps when there is no way for you in code to access them any more.  In this case once the array is null, then you can't access the elements in the array so the whole thing gets garbage collected.

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

I think loading them as resources is essentially equivalent to having them on the local filesystem - it's another way to store the image.

I was assuming that the images were being initially loaded over the network and hence why there was a noticeable delay in getting them loaded?

Rohit BajajAuthor Commented:
The wallpapers are loaded from local disk only not over the network.
However the retreiving the Bitmap from the wallpaperName is taking around 2 seconds.
I am using the following function to retrieve the Bitmap image :

 public static Bitmap getBitmapFromWallpaperAsset(Context context, String wallpapername) {
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open("wallpapers/" + wallpapername);
        } catch (IOException e) {
        return BitmapFactory.decodeStream(inputStream);
I think loading them as resources is essentially equivalent to having them on the local filesystem - it's another way to store the image.
I think i muddied the waters slightly, sorry. Actually it's loading them via
that caches them. They basically recommend that if you want a lean and sophisticated cache that you make your own. But you could use the above if all you need is something simple
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.