(JAVA) out of memory error

This is a discussion on (JAVA) out of memory error within the Tech Board forums, part of the Community Boards category; OK I'm implementing a data structure called "trie" to store a dictionary of words. The text file I'm reading in ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    (JAVA) out of memory error

    OK I'm implementing a data structure called "trie" to store a dictionary of words. The text file I'm reading in has over 172K words....this, with a good implementation of a trie should not take more than 20-22MBs of memory. For some reason my implementation gives me an OutOfMemoryError.

    If I use a small dictionary....say 50K words everything is fine...any ideas?

    BTW...I'm passing a hashedMap set to add.
    Code:
    class TrieNode {
        TrieNode[] myLinks;
        boolean myIsWord;
        String myWord;
    
        /**
         *  create a new TrieNode (characters a-z and ' ')
         */
        public TrieNode() {
            myLinks = new TrieNode[Trie.ALPH];
            myIsWord = false;
            myWord = null;
        }
    
    }
    
    public class Trie {
    
        TrieNode root;
        public static final int ALPH = 27;
    
        public Trie() {
            root = new TrieNode();
        }
    
        /**
         * Add a string to the trie
         *
         * @param s The string added to Trie
         */
        public void add(String s) {
            TrieNode t = root;
    
            int limit = s.length();
            for(int k=0; k < limit; k++) {
                int index = index(s.charAt(k));
                if (t.myLinks[index] == null) {
                    t.myLinks[index] = new TrieNode();
                }
                t = t.myLinks[index];
            }
            t.myIsWord = true;
            t.myWord = s;	
        }
    
        /**
         * print every word in the trie, one per line
         *
         */
        public void printTrie() {
            printTrie(root);
        }
    
    
        private void printTrie(TrieNode t) {
            if (t != null) {
                if (t.myIsWord) {
                    System.out.println(t.myWord);
                }
    
                for(int k=0; k < ALPH; k++) {
                    if (t.myLinks[k] != null) {
                        // System.out.println("Descend to child " + letter(k));
                        printTrie(t.myLinks[k]);
                    }
                }
            }
        }
    
        /**
         * determine if a word is in the trie (starting at root)
         *
         * @param s The string searched for
         * @return true iff s is in trie (starting at root)
         */
        public boolean contains(String s) {
            TrieNode t = root;
    
            int limit = s.length();
            for(int k=0; k < limit; k++) {
                int index = index(s.charAt(k));
                if (t.myLinks[index] == null) return false;
                t = t.myLinks[index];
            }
            return t.myIsWord;
        }
    
        // convert all unknown symbols to spaces
        public static int index(char ch) {
            int i = (int) (ch - 'a');
    
            if ((i < ALPH-1) && (i >= 0))
                return i;
            else
                return ALPH-1;
        }
    
        public static char letter(int i) {
            if (i == ALPH-1)
                return ' ';
            else
                return (char) (i + 'a');
        }
    
    
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm no Java expert, but there may be a limit in how much memory you can use.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    hmm...does anyone know how I can track memory consumption while debugging for example? I'm using JBuilderX

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    This seems a lot like an error a professor of mine was receiving on a sample program he showed us.

    He was showing us an example of a fibbonacci series coded in the most memory-inefficient way possible. the java runtime would throw an out-of-memory error very quickly after the program began execution.

    he increased the stack (or maybe heap) size allocated by the java runtime system and the program ran longer...

    i forgot how he did it, but he changed some environtment variable...

    try looking into that
    My Website

    "Circular logic is good because it is."

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well I just succefully read in a littlebit over 160K words, and the process took about 4seconds on a P31000mghz 256ram I tried with 165K and it failed.

    I can't seem to find the place to raize the stack size in borland...but isn't the JSDK default stack size 256Meg anyways?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    try adding "-Xmx256m" to your compile-time command line. (or whatever number you want in MB)

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    >>try adding "-Xmx256m" to your compile-time command line.

    yeah I've tried that yesterday and it didn't work. I guess something is messed up with my laptop because everything works fine on my (even older) desktop. Thanks for the help though.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    does the error get thrown from jbuilder, or from the runtime environment? there could be a bug in the version of the jre you're running.
    PHP and XML
    Let's talk about SAX

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    >>does the error get thrown from jbuilder, or from the runtime environment? there could be a bug in the version of the jre you're running.

    JBuilder throws the error...sometimes main also throws a fatal exception and it terminates, but that happened less than 25% of the time.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by axon
    >>try adding "-Xmx256m" to your compile-time command line.

    yeah I've tried that yesterday and it didn't work. I guess something is messed up with my laptop because everything works fine on my (even older) desktop. Thanks for the help though.
    are you running the same JVM on both machines?

    You can also try adding "-Xms256m" as a command line option. (youd probably want a smaller number than 256 though )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21