13 Replies Latest reply on Jul 8, 2003 6:20 AM by Sven Baumgarten

    How does memory management work?

    Sven Baumgarten Newbie

      Hi everyone,

      I am confused about how to configure jboss (3.2.1). I have a j2ee app with a java client. The client calls session bean methods for business functions (mostly create or update) and the session bean controls the entity beans. The client does about 100 to 1000 method calls a minute. The problem is that after some minutes I always get OutOfMemory Exception. I tried to increase the -Xmx option of JVM but but it only takes a little bit longer until OutOfMemory happens. It is obviously that jboss needs more and more memory with every new created entity. So I tried to change the container configuration. There was no difference between commit option B and C - still getting OutOfMemory. Then I've been playing with cache-policy-conf and container-pool-conf but I can't determine any real difference.

      The idea is to limit container cache and pool. All my entities are in the same container (Standard CMP 2.x). While calling a session bean method from the client a new transaction would be started (because all methods are "required") and every bean needed for this transaction ist getting active. After transaction is completed the beans could be pooled. The number of pooled beans is limited so that pool doesn't exceed a certain amount of memory. If the pool is full some beans have to be destroyed. The number of active beans is also limited because every transaction (client session method call) will end before the next is started. So it depends on how much clients are calling simultaneously.

      Does anybody know how to configure that? Or is there an other way to prevent from OutOfMemory?

      Regards,
      Sven

        • 1. Re: How does memory management work?
          Adrian Brock Master

          The pool has little effect on memory usage.
          It is just a bunch of ready to use bean
          instances.

          Your problem will be the default cache size
          <max-capacity>1000000</max-capacity>

          That is 1 million bean instances per entity bean.

          Regards,
          Adrian

          • 2. Re: How does memory management work?
            Sven Baumgarten Newbie

            Hi Adrian,

            I've made some test the last few days. First of all I limited the cache from 10 to 50 beans. but there was no real improvement. Then I did some memory monitoring with JMP (Java Memory Profiler).

            There are 2 Entity Beans "OrderBean" and "OrderItemBean" connected by 1:N relation an a Session Bean "OrderCtrlBean". The OrderCtrl has the following method:

            1 public void testReadOrderItem(int id) {
            2 try {
            3 InitialContext ctx;
            4 ctx = new javax.naming.InitialContext();
            5 Object obj = ctx.lookup("ejb/OrderLocal");
            6 OrderLocalHome orderHome = (OrderLocalHome) PortableRemoteObject.narrow(obj, OrderLocalHome.class);
            7
            8 OrderLocal order = orderHome.findByPrimaryKey(new Integer(id));
            9
            10 Iterator it = order.getOrderItem().iterator();
            11 while (it.hasNext()){
            12 OrderItemLocal orderItem = (OrderItemLocal) it.next();
            13 System.out.println(orderItem.getId());
            14 }
            15 }
            16 catch (Exception ex) {
            17 ex.printStackTrace();
            18 }
            19 }

            In the test database every order has two items.

            At start there are no instances of OrderBean and OrderItemBean. After first call of this method there are two instances of "OrderBean$Proxy" (OrderBean) and "OrderItemBean$Proxy" (OrderItemBean). The first instance of OrderBean is created at line 8 and the second one at the first call of line 10 (I don't know why this). The instances of OrderItemBean are created at line 12.
            On Every call of the method testReadOrderItem two OrderBean instances and two OrderItemBean instances are created until 51 instances of each class are reached. Then somthing strange happens. If the method is called again one instance of OrderBean would be created and no more instances of OrderItemBean.
            In this way more and more instances of OrderBean are created and this fills the memory. Garbage Collection does not remove any instance of OrderBean.

            Why are so many OrderBean instances created?

            Regards
            Sven

            • 3. Re: How does memory management work?
              Adrian Brock Master

              The finder needs an instance of OrderBean
              that is not associated with a primary key.

              I would expect this to be served from the pool
              rather than creating a new one everytime.
              The 1 in the 51 (50 are in cache).

              Can you post your simple test and config?

              Regards,
              Adrian

              • 4. Re: How does memory management work?
                Sven Baumgarten Newbie

                Hi Adrian,

                here is my test project. JBossTest.zip contains the complete JBuilder project. JBossTest.jar is the server component and contains the deployment descriptors. In JBossTestClient.java you have to change the IP "192.168.1.36" to the IP of your JBoss server. The configuration files are in JBoss_Conf.zip. The EJB use default database connection. JBossTestDatabase_MSSQL2000.sql contains the database creation skript for MS SQL Server 2000.
                The client JBossTestClient first creates order objects (method writeOrders). This works as expected. Second the client reads the orders (method readOrders). This is where the problem is.

                Java Memory Profiler: http://www.khelekore.org/jmp

                JBoss start with Java Memory Profiler: C:\Programme\Java\j2sdk1.4.1\bin\java -Xms64m -Xmx128m -classpath C:\Programme\Java\j2sdk1.4.1\lib\tools.jar;C:\jboss-3.2.1\bin\\run.jar -Xrunjmp:nomonitors;nomethods:filter=jbosstest org.jboss.Main

                • 5. Re: How does memory management work?
                  Sven Baumgarten Newbie

                  Hi Adrian!

                  did you find out everything? I would be very satisfied if you can give a short feedback if you could retrace my problem.

                  Regards,
                  Sven

                  • 6. Re: How does memory management work?
                    Adrian Brock Master

                    I don't have JBuilder or MSSQL so I left it until
                    I felt like converting it. But it has dropped off
                    my radar.
                    Could you convert it to run under a default jboss
                    installation, i.e. a ready to deploy ejb application
                    and hsqldb.

                    Regards,
                    Adrian

                    • 7. Re: How does memory management work?
                      Sven Baumgarten Newbie

                      JBossTest.jar in JBossTest.zip is ready for deployment. You only have to copy it into the deploy folder. It uses the default db-connection defined in standardjbosscmp-jdbc.xml. JBossTestClient.java is the client.

                      Regards,
                      Sven

                      • 8. Re: How does memory management work?
                        Adrian Brock Master

                        I had lots of fun trying to get your testcase to work with
                        hsqldb. Catching Exception, dumping the trace and continuing
                        is not recommended practice.

                        Anyway, I don't see the problem you state. When I set the
                        cache to 50, there are 51 OrderItems and Orders.
                        50 for the cache (commit-option B) and one each in your
                        EJBLocalHome factory.

                        You did run the garbage collector?

                        Regards,
                        Adrian

                        • 9. Re: How does memory management work?
                          Sven Baumgarten Newbie

                          No i did not. Do I have to run the garbage collector myself?

                          Regards,
                          Sven

                          • 10. Re: How does memory management work?
                            Adrian Brock Master

                            It is non-determinstic when the gc runs.
                            It does make a best effort to remove all garbage
                            before throwing OutOfMemory

                            Regards,
                            Adrian

                            • 11. Re: How does memory management work?
                              Sven Baumgarten Newbie

                              I did the test again with the source code i posted. I only reduce the for iteration in the client from 2500 to 500. In the attached word document you can see the results. Writing works correctly and reading not. I made the screen shots always after running gc (autmatically by the server).

                              Regards,
                              Sven

                              • 12. Re: How does memory management work?
                                Adrian Brock Master

                                I see the same for both tests, although I only had 50
                                in my cache and 100 in my test.

                                I am using jboss3.2.2RC2 from CVS rather than 3.2.1

                                Regards,
                                Adrian

                                • 13. Re: How does memory management work?
                                  Sven Baumgarten Newbie

                                  That's it! I have downloaded jboss3.2.2RC1 binary and now it works.

                                  Thank you very much for your help.

                                  Regards,
                                  Sven