1 Reply Latest reply on Dec 10, 2012 11:29 AM by Jon Tara

    Failed to allocate memory

    Marcus Atkins

      Below is a method which inserts records into the devices database. I am having a problem where I get a 'failed to allocate memory' error.

       

      There are 10 models, one is reasonably large with 108,000 records.

       

      The error occurs when executing this line (f.readlines().each do |line|) but it occurs after the largest model has already been inserted.

       

      Is the memory not being released by the block that is iterating through the lines? Or is there something else happening?

       

      Any help on this matter would be greatly appreciated!

       

       

      def insertRecordsIntoRhom(models)

             WebView.execute_js("update_progressLabel('Updating:','progressLabel#{4}',1)")

       

           updateAmount = 45 / models.length

          

           GC.enable

       

            models.each_with_index do |model,i|

              csvColumns = Array.new

       

       

              #make sure the model has been loaded

              eval("#{model}.find(:first)")

              db = ::Rho::RHO.get_src_db(model)

              app_error("current model: " + model)

              db.start_transaction

              begin 

      j=0

      WebView.execute_js("update_progressLabel('#{models[i]}...','progressLabel#{5+i}',1)")

            

           

              #File.read("#{model}.csv").each_line do |line|

              f = File.new("#{model}.csv")

              f.readlines().each do |line|

                

                      #extract columns from header line of csv

      if j==0

      csvColumns = getCsvFieldFromHeader(line)

      j+=1

      next

      end 

                

                

      #parse the csv line

      #recordToCreate = csvPutIntoHash(line,csvFields)#good for debugging

      eval(models[i] + ".create(#{csvPutIntoHash(line,csvColumns)})")

              

              end

             

              f.close

              db.commit

              app_error("commited model: #{model}")

              rescue

                db.rollback

              end

            

              #update progress bar and progress label

              WebView.execute_js("update_progressBar('" + (55 + (updateAmount * (i + 1))).to_s + "','progressBar');")

              WebView.execute_js("update_progressLabel('Done','progressLabel#{5+i}',0)")

            end

       

        end