Results 1 to 3 of 3
  1. #1
    arnab321 is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default 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?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    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.

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power

    Default 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:

    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);
    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.

Similar Threads

  1. accessing hashtables from another class
    By SimC in forum New To Java
    Replies: 19
    Last Post: 12-05-2008, 05:49 PM
  2. Replies: 2
    Last Post: 08-21-2008, 08:33 PM
  3. JVM memory usage
    By lardum in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 04:30 AM
  4. Latest XML parsing and memory usage benchmark
    By Jimmy Zhang in forum XML
    Replies: 0
    Last Post: 03-03-2008, 10:49 PM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 11:43 PM

Posting Permissions

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