| Reply | « Previous Thread | Next Thread » |
|
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?
|
|
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. |
|
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? |
| gamesprite |
| View Public Profile |
| Find all posts by gamesprite |
|
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 |
|
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? |
|
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 | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|