title: Flask not releasing memory
body: <p>I'll begin this question with pointing out that I'm very new to web development. </p>
  
  <p>Now to the point: I've recently built a rather small <code>Flask</code> application that loads data and then outputs visualizations of it using <code>bokeh</code>. And as such it will have to keep quite a lot of data stored in memory (about 10-20 mb). This in itself is not really a problem, however, the application does not release the objects in memory after the view functions have sent the requests. Which means that after just a few uses the app will have drained most of the memory.</p>
  
  <p>My question is therefore: How do I force <code>Flask</code> to release the used objects after the view functions have returned any requests? Or am I going about this problem the wrong way? It could be worth mentioning that I use the built in server of <code>Flask</code> as we're still only prototyping. 
  ¨
  Thanks, Tingis</p>
  
  <p><strong>EDIT</strong> Here's one of my view functions. What it does is that it loads data from a db using <code>SQLAlchemy</code> and then does some timeseries manipulations (such as a kernel density estimate and calculating cumulative return) and outputs a joined <code>div</code> and <code>script</code> string of a <code>bokeh</code> figure, which are the variables ending with <code>_plt</code>. </p>
  
  <pre><code>from app import app
  from app.business_logic.classes.interface_classes import Company
  from app.business_logic.functions.functions import get_session
  
  @app.route('/analysis_tool/company_performance', methods=['GET', 'POST'])
  def analysis_tool__company_performance():
      session = get_session()
  
      companies_to_analyse = {
          'Company A': {'ts_to_use': 'Returns of Company A'},
          'Company B': {'ts_to_use': 'Returns of Company B'}
      }
  
      chosen_company = request.form.get('security')
      types_of_plots = {}
  
      if chosen_company is not None:
          company = Company(session, chosen_company)
          company.load_company()
  
          company.load_timeseries(companies_to_analyse[chosen_platform]['ts_to_use'])
          company.unpack_observations_of_ts_as_df()
  
          ret_df = company.manipulate_dataframe('convert timeseries to returns',
                                                 frequency='monthly',
                                                 ts_type=company.loaded_ts_orm_obj.ts_type_name)
          cum_ret_df = company.manipulate_dataframe('calculate cumulative return', df=ret_df)
  
          cum_ret_plt = company.plot_dataframe(cum_ret_df, legend=False)
          kde_plt = company.plot_kde(ret_df)
  
          types_of_plots = {'Cumulative_return': cum_ret_plt, 'KDE': kde_plt}
  
      return render_template('plotting/plot_using_panels.html',
                             items=sort_dictionary(platforms_to_analyse),
                             plot_types=sort_dictionary(types_of_plots),
                             selected=chosen_company)
  </code></pre>
  
  <p>Does this help?</p>
  
  <p><strong>EDIT 2</strong> I tried the solution that was provided in the following <a href="https://stackoverflow.com/questions/7740832/need-help-identifying-a-memory-leak-involving-matplotlib-and-flask">question</a>, by adding a <code>gc.collect()</code> after each call to the plotting interface, as well as the function</p>
  
  <pre><code>@app.teardown_request
  def teardown_request(exception):
      gc.collect()
  </code></pre>
  
  <p>But the memory still isn't released.</p>