title: Selenium webdriver eats up all memory in loop
body: <p>I have problem with processes and memory usage. I created a script to parse some webpages using selenium webdriver with PhantomJS. The script works fine, but my code need some optimization.</p>
  
  <p>I start script and after a moment, my memory is full because I have a lot of phantomjs processes.</p>
  
  <p><strong>How to fix this?</strong> my code:</p>
  
  <p>server.py</p>
  
  <pre><code>from selenium import webdriver
  import radio
  import urllib2
  class Server(object):
      running = False
      radio_dir = "db/radio.txt"
      def __init__(self):
          """Choose browser from selenium webdrivers"""
          self.browser2 = webdriver.PhantomJS()
          self.r = radio.RadioParser(self.browser2)
          self.running = True
      def loop(self):
          """main server loop"""
          while self.running:
              radio_str = self.r.parse()
              self.save(self.radio_dir, radiozet_str)
              time.sleep(30)
      def save(self, location, string):
          """Put location and string to override db files"""
          try:
              file = open(location, 'w')
              file.write(string)
          except IOError:
              print "IOError"
          finally:
              file.close()
  
  if __name__ == "__main__":
      s = Server()
      s.loop()
  </code></pre>
  
  <p>and radio.py</p>
  
  <pre><code>class RadioParser(object):
      url = "http://www.radiourl/index.html"
      def __init__(self, browser):
          self.driver = browser
      def parse(self):
          self.driver.get(self.url)
          song = (self.driver.find_element_by_class_name("artist").text +u" - " +
                  self.driver.find_element_by_class_name("album").text)
          print song
          return str(song)
  </code></pre>