4 Replies Latest reply: Jul 25, 2013 10:59 AM by greybird RSS

    My service ran out of memory a day

    cc1453ce-7b3d-49df-8b40-670fe8adc809

      hello,

      My service use bdb java edition. More than 10,000 people use it,Service runs one day, out of memory. Service Unavailable.

      my code as follow:

      //This code has only been called once during the initialization
           public void create(String dbPath){
               File bdbPath = new File(dbPath+"/bdb");
               if(!bdbPath.exists()){
                 bdbPath.mkdirs();
               }
        
               /* Create a new, transactional database environment */
               EnvironmentConfig envConfig = new EnvironmentConfig();
               envConfig.setTransactional(true);
               envConfig.setAllowCreate(true);
               envConfig.setCacheMode(CacheMode.EVICT_LN);
               envConfig.setCachePercent(75);
               exampleEnv = new Environment((bdbPath), envConfig);
               //set txn
               TransactionConfig transactionConfig = new TransactionConfig();
               transactionConfig.setReadUncommitted(true);
        
               Transaction envsnInfoTxn = exampleEnv.beginTransaction(null, transactionConfig);

               Transaction myClassTxn =  exampleEnv.beginTransaction(null, transactionConfig);
               //set dbConfig    
               DatabaseConfig dbConfig = new DatabaseConfig();
               dbConfig.setTransactional(true);
               dbConfig.setAllowCreate(true);
               dbConfig.setCacheMode(CacheMode.EVICT_BIN);
               //make sure the key is unique
               dbConfig.setSortedDuplicates(false);
               envsnDb =  exampleEnv.openDatabase(envsnInfoTxn, "envsnDb", dbConfig);
               myClassDb =  exampleEnv.openDatabase(myClassTxn, "myClassDb", dbConfig);
               //set seq
               SequenceConfig config = new SequenceConfig();
               config.setAllowCreate(true);
               DatabaseEntry keyEntry = new DatabaseEntry();
               StringBinding.stringToEntry(KEY_NAME, keyEntry);
               envsnSeq = envsnDb.openSequence(envsnInfoTxn, keyEntry, config);
              //txn commit
              envsnInfoTxn.commit();
              myClassTxn.commit();
           }

      //my business as follow

      public String getNewEnvsnInfo(String oldid)
         throws StoragerException {
         String newid = "";
         TransactionConfig transactionConfig = new TransactionConfig();
         transactionConfig.setReadUncommitted(true);
         //begin txn
         Transaction txn = exampleEnv.beginTransaction(null, transactionConfig);
        
         DatabaseEntry keyEntry = new DatabaseEntry();
         DatabaseEntry dataEntry = new DatabaseEntry();
        
         EnvsnInfo tmpEnvsnInfo = new EnvsnInfo();
         EnvsnInfo newEnvsnInfo = new EnvsnInfo();

         // Binding entity
         StoredClassCatalog classCatalog = new StoredClassCatalog(myClassDb);
         EntryBinding dataBinding = new SerialBinding(classCatalog,EnvsnInfo.class);
         //set cursor
         CursorConfig config = new CursorConfig(); 
         config.setReadUncommitted(true);
         Cursor cursor = envsnDb.openCursor(null, config);
         String id = "";
        
         StringBinding.stringToEntry(oldid, keyEntry);

         //find ID
         if(cursor.getSearchKey(keyEntry, dataEntry, LockMode.DEFAULT)== OperationStatus.SUCCESS){
          tmpEnvsnInfo = (EnvsnInfo)dataBinding.entryToObject(dataEntry);
          newid = tmpEnvsnInfo.getNewid();
          cursor.close();
          txn.commit();
          return newid;
         }else{

          // not found ,use seq
          long sq = envsnSeq.get(txn, 1);
          int b = new Long(sq).intValue();
          id = String.format("%07d", b);
         
         }
         newEnvsnInfo.setId(id);
         newEnvsnInfo.setOldid(oldid);
        
         newid = id;
         newEnvsnInfo.setNewid(newid);
         StringBinding.stringToEntry(oldid, keyEntry);
         dataBinding.objectToEntry(newEnvsnInfo, dataEntry);

         // save envsn
         OperationStatus status =  envsnDb.put(null, keyEntry, dataEntry);
         if (status != OperationStatus.SUCCESS) {
          cursor.close();
          txn.commit();
          throw new StoragerException("Save Envsn info got status " + status);
         }
         cursor.close();
         txn.commit();
         return newid;
        }


          exampleEnv and  envsnDb never closed.

          I use Eclipse Memory Analyzer to analyze memory.Found that com.sleepycat.je.tree.BIN takes up a lot of memory,Leads to memory overflow.

          

       

         Why is it so?

      Thanks in advance.