Results 1 to 3 of 3
- 12-07-2008, 09:34 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 40
- Rep Power
- 0
should i use arrays or hashtables for less memory usage?
static String[][] sinfo=new String[7][700];
static String[][] songs=new String[65][4];
static InputStream[] is=new InputStream[40];
well, im using many arrays like this. and as the variables are gradually filled, i get an OutOfMemoryException. so, should i use hashtables? how does using hashtables differ from arrays?
- 12-07-2008, 02:42 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Whether as arrays or hashtables the problem will be the same. The amount of space the structure itself uses is negilgable, it is the amount of data your storing.
Increase your heap size with the -Xmx option.
- 12-07-2008, 06:13 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Hash tables useful for 'sparse' arrays
Just to expand slightly on the previous comment: hash tables are sometimes useful for "sparse" arrays where you have a large array but only a small percentage of the elements are actually filled. For examle, let's say we have some data that could be indexed on 2 numbers in the range 1-1000 (total 1 million possible combinations), but in practice only about 5000 of the spaces are filled. So instead of something like this:
String[][] arr = new String[1000][1000];
you can declare e.g. a HashMap and use as the key a number which combines the two indices:
To combine the numbers into one key, I'm multiplying one by a prime and then adding the other; there are different possibilities, and if you were really stuck you could even combine them into a String -- with obvious but possibly acceptable overhead.Java Code:Map<Integer, String> m = new HashMap<Integer, String>(5000); ... void putAt(int x, int y, String str) { int key = 89 * y + x; m.put(key, str); }Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
accessing hashtables from another class
By SimC in forum New To JavaReplies: 19Last Post: 12-05-2008, 04:49 PM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
JVM memory usage
By lardum in forum New To JavaReplies: 7Last Post: 06-26-2008, 03:30 AM -
Latest XML parsing and memory usage benchmark
By Jimmy Zhang in forum XMLReplies: 0Last Post: 03-03-2008, 09:49 PM -
Generic Hashtables
By ShoeNinja in forum New To JavaReplies: 0Last Post: 12-04-2007, 10:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks