You Are Here:

Community: Developer Discussion Boards

#1 Old jar resources vs. hardcoding - 2004-04-22, 16:09

Join Date: Jan 2004
Posts: 49
Location: Poland
ooooooo
Offline
Registered User
I have dilemma which is better (memory/efficiency advantiges): hardcoding data (e.g. byte arrays, lots of Strings ...) or keeping them in jar. One remark - providing they are needed by application almost all the time?
Reply With Quote

#2 Old 2004-04-23, 03:15

Join Date: Mar 2003
Posts: 16
Location: Zurich, Switzerland
nokiafin
Offline
Registered User
static final member variables are (supposedly) the fastest in access time (except local variables on the stack).
Putting your data into the class (as static final member) seems the better way of doing it, performance-wise. Reading your data from a recources file through an Inputstream requires file handling etc. which takes up memory from the heap /and/ consumes time.

In the jar, both solutions will take up pretty much (if not exactly) the same amount of space, in the end jar is just zip and your data is encoded the same way in both cases.
Reply With Quote

#3 Old 2004-04-23, 04:50

Join Date: Mar 2003
Posts: 60
gamesprite
Offline
Regular Contributor
hardcoding the resource maybe is not a good idea.
supporse you have a class like that:
public class Res
{
static final byte image1Byte[]={.....};//the byte array of the image1
static final byte image2Byte[]={.....};//the byte array of the image2
//and so on
};

so when you only want to create Image image1 ,you have to write
Image image1 = Image.createImage(Res.image1Byte,0,Res.image1Byte.length());

that means the java VM has to load the whole Res.class into the heap to create the image1.this will probably result in the exeception of out of memory.

Is that right?
Reply With Quote

#4 Old 2004-04-23, 22:17

Join Date: Mar 2003
Posts: 148
Location: USA, FL
dpolyakov
Offline
Game Artists
I think that way:
If you have a constant data - do it in class.
If your data can be changed from phone to phone - do it in resources.

Dima
Game Artists, LLC
www.GameArtists.com
Reply With Quote

#5 Old 2004-04-24, 11:03

Join Date: Jan 2004
Posts: 49
Location: Poland
ooooooo
Offline
Registered User
Thanks guys.
My data are constant and I did not even think of creating new class just to store them - just table variables in existing ones. My main concern are memory constraints of older devices - there could be shortages there. I may be wrong but I am affraid of "duble class file size". I never got deeper, but as I assume that classes are loaded in device's memory twice !? On the other side resources in jar take some memory and when they are loaded and written as e.g byte tables - memory consumption doubles - so again - which is less memory eager?
Reply With Quote

#6 Old IMHO - 2004-04-27, 18:37

Join Date: Mar 2003
Posts: 31
Location: London
boinged
Offline
Registered User
If you are using ints or Strings (not arrays) then use:

private static final int MY_CONSTANT = 20;
etc.

A decent obfuscator will then replace these constants by their literal values.

If you are using arrays, ALWAYS put it in a file, this is because the following:

private static final byte[] DATA = {0,1,2,3,4};

actually gets compiled into:

DATA=new byte[5];
DATA[0]=0;
DATA[1]=1;
DATA[2]=2;
DATA[3]=3;
DATA[4]=4;

(you can check this using a java decompiler)

So you can imagine that if you have a large byte array then to initialise this in the same way will use a lot of JAR and heap.

You do need to be aware that if you load this as a file, then you need enough heap to store the entire file, plus the byte array you are loading it into. E.g in the above case you would need 10 bytes free while loading the file.

After you've loaded the data you can throw away the file. This leads to another problem in that you now have hole in the memory. This isn't the end of the world but you do need to be careful.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia