derby - java.lang.OutOfMemoryError Java heap space

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamborta
    New Member
    • Jul 2008
    • 7

    derby - java.lang.OutOfMemoryError Java heap space

    im trying to iterate through a large database (over 5000 entries), and printing out the entries
    using this code:
    [code=java]
    String sql = "SELECT "+column+ " ,clickurl from " + tableName;
    stmt = conn.prepareSta tement(sql);
    ResultSet results=stmt.ex ecuteQuery();

    while(results.n ext()){

    String clickurl = results.getStri ng(2);
    System.out.prin tln(" Test - " + clickurl);
    }
    [/code]

    but after the 4000. entry java runs out of memory.

    i dont know if i understand the system correctly, but the results are stored in the a ResultSet variable already, and iterating through it shouldn't require more memory.

    i can't figure out what the problem is.
    Last edited by Atli; Jul 16 '08, 12:50 AM. Reason: Added [code] tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Seeing as this is more of a Java problem than a MySQL problem, I've moved it over to the Java Forum.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by jamborta
      im trying to iterate through a large database (over 5000 entries), and printing out the entries
      using this code:
      [code=java]
      String sql = "SELECT "+column+ " ,clickurl from " + tableName;
      stmt = conn.prepareSta tement(sql);
      ResultSet results=stmt.ex ecuteQuery();

      while(results.n ext()){

      String clickurl = results.getStri ng(2);
      System.out.prin tln(" Test - " + clickurl);
      }
      [/code]

      but after the 4000. entry java runs out of memory.
      It will probably help, to put[code=java]String clickurl = "";[/code]before the loop and just[code=java]clickurl = results.getStri ng(2);[/code]in it. That way, you don't create a new string every time the loop is called, instead it's overwritten. Otherwise, the garbage collector would have to clean it up and it doesn't seem to be doing that in time.
      Originally posted by jamborta
      i dont know if i understand the system correctly, but the results are stored in the a ResultSet variable already, and iterating through it shouldn't require more memory.
      I think you're wrong with that. As I understand it, you're creating a new copy of a string every time the loop is called. It's like[code=java]public class CompareStrings {
      public static void main(String[] args) {
      StringWrapper sw = new StringWrapper() ;
      String hi = sw.getWord();
      System.out.prin tln(hi == sw.hello);
      }
      }

      class StringWrapper {
      public String hello = "Hello";
      public String getWord() {return new String(hello);}
      }[/code]Check it, it will return false. That's why your program runs out of memory.

      Of course, you could just do this:[code=java]while(results.n ext()){

      System.out.prin tln(" Test - " + results.getStri ng(2));
      }[/code]That won't work of course, if you need the String clickurl somewhere else.

      Greetings,
      Nepomuk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by nepomuk
        It will probably help, to put[code=java]String clickurl = "";[/code]before the loop and just[code=java]clickurl = results.getStri ng(2);[/code]in it. That way, you don't create a new string every time the loop is called, instead it's overwritten. Otherwise, the garbage collector would have to clean it up and it doesn't seem to be doing that in time.
        The gc always runs when it is desparately needed; one local String doesn't
        increase the consumed memory much; the gc will clean it up. I use Derby
        myself and just conducted a small test: 100,000 rows without any problems
        so I guess the problem is somewhere else ...

        kind regards,

        Jos

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          ps. probably your cache size is too small; e.g. set this property:

          [code=text]
          derby.storage.p ageCacheSize = 4M
          [/code]

          kind regards,

          Jos

          Comment

          • jamborta
            New Member
            • Jul 2008
            • 7

            #6
            hi,
            thanks for the suggestions. i tried to run it without creating a string, but it still runs out of memory, although slightly later.
            where can i set derby.storage.p ageCacheSize?

            thanks

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by jamborta
              where can i set derby.storage.p ageCacheSize?
              Put it in your System.properti es before you start Derby or use a command line
              parameter -Dderby.storage. pageCacheSize=4 M

              kind regards,

              Jos

              Comment

              • jamborta
                New Member
                • Jul 2008
                • 7

                #8
                Hi Jos,

                thanks for the reply, I added these lines (-J-Dderby.storage. pageSize=8m -J-Dderby.storage. pageCacheSize=8 m) to netbeans.conf, it shows up as an argument, but doesnt seem to effect the application. it still runs out of memory, i was trying to play with the numbers, but nothing happened.

                (i have 4gb memory assigned 512/512 heap/non heap memory, that should be enough i guess)

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by jamborta
                  thanks for the reply, I added these lines (-J-Dderby.storage. pageSize=8m -J-Dderby.storage. pageCacheSize=8 m) to netbeans.conf, it shows up as an argument, but doesnt seem to effect the application. it still runs out of memory, i was trying to play with the numbers, but nothing happened.
                  I suspect the bug to be somewhere else; as I wrote, I did a little test with 100,000
                  rows and it ran fine, i.e. insert them and select them all. Would it be possible to
                  show a bit of (relevant) code? Or, alternatively, use the Runtime.freeMem ory()
                  method in the relevant portions of your code and see what happens; also make
                  sure that you close Derby's resources (result sets, statements etc.)

                  kind regards,

                  Jos

                  Comment

                  Working...