Rohit Bajaj
asked on
Should one use static variables or store data in database
Hi,
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 ?
Thanks
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 ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Doug
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.
Doug
If you load your images as resources into an array, you should be OK as Java caches resources by default
http://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans
http://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse
http://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans
http://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse
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?
Doug
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?
Doug
ASKER
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 getBitmapFromWallpaperAsse t(Context context, String wallpapername) {
InputStream inputStream = null;
try {
inputStream = context.getAssets().open(" wallpapers /" + wallpapername);
} catch (IOException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream (inputStre am);
}
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 getBitmapFromWallpaperAsse
InputStream inputStream = null;
try {
inputStream = context.getAssets().open("
} catch (IOException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.