Java out of memory exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 123456prakash
    New Member
    • Jan 2007
    • 35

    Java out of memory exception

    Hi all ,
    I am new to this . This is my first post . I really need some help from you all members of this forum.
    My situation is something like this .
    I am communicating with 2 system .Both the systems are in LAN . In the first system (Windows xp) my java application and database (MYSQL 5.0) is running.
    My second system is my linux machine which has an applcation running which sends some xyz information for every packet which it receive to my java application.It is configurable for example if I give tcp packets then it will send information only when TCP packet is recived .Similarly If I give ICMP packets then it will send information only when ICMP packet is recived ... .

    What i am doing is i am reading the information from the socket and inserting into some table in the database. Some time when the traffic over network is high I am getting around 10000 messages per second.
    So what i m doing is i am adding the message as an string object to a vector. And i have a thread running which for every 2 seconds read that vector and perform a bulk insertion to the database with the values in that vector and then empty the vector.
    This logic is working properly but sometimes when traffic is very high then I get runtime exception ie java out of memory exception.
    How to solve this problem please can anyone help me out.

    Thank you for your response
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you can increase the JVM heap size, see discussion
    http://forum.java.sun. com/thread.jspa?thr eadID=490356&ts tart=0

    but the problem may still occure

    could you use a double buffer? be filling one buffer which another thread is writing the other buffer - you can then test to see if first buffer is full before the second is writen - if so you can think about solving that problem, e.g. may need faster machine, faster disk, bigger disk cache?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by 123456prakash
      Hi all ,
      I am new to this . This is my first post . I really need some help from you all members of this forum.
      My situation is something like this .
      I am communicating with 2 system .Both the systems are in LAN . In the first system (Windows xp) my java application and database (MYSQL 5.0) is running.
      My second system is my linux machine which has an applcation running which sends some xyz information for every packet which it receive to my java application.It is configurable for example if I give tcp packets then it will send information only when TCP packet is recived .Similarly If I give ICMP packets then it will send information only when ICMP packet is recived ... .

      What i am doing is i am reading the information from the socket and inserting into some table in the database. Some time when the traffic over network is high I am getting around 10000 messages per second.
      So what i m doing is i am adding the message as an string object to a vector. And i have a thread running which for every 2 seconds read that vector and perform a bulk insertion to the database with the values in that vector and then empty the vector.
      This logic is working properly but sometimes when traffic is very high then I get runtime exception ie java out of memory exception.
      How to solve this problem please can anyone help me out.

      Thank you for your response
      If you can post the code for storing in vector and reading the vector and inserting to database perhaps there could be ways of optimizing it.

      Comment

      • 123456prakash
        New Member
        • Jan 2007
        • 35

        #4
        Originally posted by r035198x
        If you can post the code for storing in vector and reading the vector and inserting to database perhaps there could be ways of optimizing it.
        Hi r035198x ,

        Thanks for your quick response
        But I cant really diverge the whole code its aganist my company policy
        but a the logic is something like this

        I have a static vector for example in class A and i have declared as below

        static Vector dataVector= new Vector();

        In one thread (ReadSocket Thread)i am filling this vector

        void run (){

        // performing operation for reading the infromation from the socket
        and storing in a string

        String message = // store the message in string object
        A.dataVector.ad d(message);

        // this thread will always be running with the highest priority and i
        have set this as dameon thread
        }


        Another thread is also (WriteToDatabas e Thread) in which i read the vector


        void run(){
        int n = A.dataVector.si ze();
        if(! A.dataVector.is Empty()){
        PreparedStateme nt db = _con.prepareSta tement ( "INSERT
        INTO messageTable (aaa,bbb,ccc,dd d,mess) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?);");

        while (n>0){
        String message = (String) A.dataVector..e lementAt(0);
        // setting the parameter ...
        db.setInt(..)
        db.setInt(..)
        db.setString(me ssage)
        // in that table there are nearly 20 feilds ...
        db.addbatch();
        n--;
        }
        da.executeBatch ();

        }

        }


        This is simple logic which i am using and there are some other threads running in the background also..

        Comment

        • 123456prakash
          New Member
          • Jan 2007
          • 35

          #5
          Originally posted by horace1
          you can increase the JVM heap size, see discussion
          http://forum.java.sun. com/thread.jspa?thr eadID=490356&ts tart=0

          but the problem may still occure

          could you use a double buffer? be filling one buffer which another thread is writing the other buffer - you can then test to see if first buffer is full before the second is writen - if so you can think about solving that problem, e.g. may need faster machine, faster disk, bigger disk cache?

          Hi horace1
          Thanks for your quick response.
          My machine is with 1 gb RAM .
          I saw that link which you have given.But there is no solution given .
          If you have any please suggest me how to increase the JVM heap size

          Thank you
          prakash

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by 123456prakash
            Hi horace1
            Thanks for your quick response.
            My machine is with 1 gb RAM .
            I saw that link which you have given.But there is no solution given .
            If you have any please suggest me how to increase the JVM heap size

            Thank you
            prakash
            have a look at
            http://java.sun.com/docs/hotspot/ism.html
            http://www.onjava.com/pub/a/onjava/2001/08/22/optimization.ht ml

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by 123456prakash
              Hi horace1
              Thanks for your quick response.
              My machine is with 1 gb RAM .
              I saw that link which you have given.But there is no solution given .
              If you have any please suggest me how to increase the JVM heap size

              Thank you
              prakash
              It is not neccessary to visit another forum for this.
              IIncreasing the available JVM heap size is simply done with the -Xmx parameter to the JVM. Type java -X to get help on using the other options

              Comment

              • 123456prakash
                New Member
                • Jan 2007
                • 35

                #8
                Originally posted by r035198x
                It is not neccessary to visit another forum for this.
                IIncreasing the available JVM heap size is simply done with the -Xmx parameter to the JVM. Type java -X to get help on using the other options

                Thank you .
                But I have another problem .. I am running my application from eclipse
                and I have to make an exe file with installsheild software and give it to a customer..

                In that exe file i compress my JVM also .
                So how to handle this situation .. Is there any way to increase JVM heap size
                at runtime

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by 123456prakash
                  Thank you .
                  But I have another problem .. I am running my application from eclipse
                  and I have to make an exe file with installsheild software and give it to a customer..

                  In that exe file i compress my JVM also .
                  So how to handle this situation .. Is there any way to increase JVM heap size
                  at runtime
                  You may need to create a platform-dependant native wrapper that launches a .exe which launches the JVM with the required heap size options.

                  Another method is to run the program using shell script or a batch but may not suit your requirements.

                  Comment

                  • vinitwagh
                    New Member
                    • Feb 2008
                    • 1

                    #10
                    Originally posted by 123456prakash
                    Hi all ,
                    I am new to this . This is my first post . I really need some help from you all members of this forum.
                    My situation is something like this .
                    I am communicating with 2 system .Both the systems are in LAN . In the first system (Windows xp) my java application and database (MYSQL 5.0) is running.
                    My second system is my linux machine which has an applcation running which sends some xyz information for every packet which it receive to my java application.It is configurable for example if I give tcp packets then it will send information only when TCP packet is recived .Similarly If I give ICMP packets then it will send information only when ICMP packet is recived ... .

                    What i am doing is i am reading the information from the socket and inserting into some table in the database. Some time when the traffic over network is high I am getting around 10000 messages per second.
                    So what i m doing is i am adding the message as an string object to a vector. And i have a thread running which for every 2 seconds read that vector and perform a bulk insertion to the database with the values in that vector and then empty the vector.
                    This logic is working properly but sometimes when traffic is very high then I get runtime exception ie java out of memory exception.
                    How to solve this problem please can anyone help me out.

                    Thank you for your response
                    hi ,
                    i read your problem, i suggest to you try some thing as follows :

                    1) try to sleeping of thread.
                    2) check whether every thread not created new connection. because when u create connection 4 objects they are created in a heap.
                    3) check once the thread finished resources must be free.
                    4)read some advanced JDBC featured like datasouce.
                    5)think about pooledConnectio n.

                    please let me know when your problem soved.
                    best of luck
                    bye

                    Comment

                    • Mohanj
                      New Member
                      • Feb 2007
                      • 2

                      #11
                      Please check the below url

                      Comment

                      Working...