title: Flask memory spike when processing big json request
body: <p>I have a flask app (flask version 1.0.2) that processes xml documents. The documents are POSTed in json format, say like this:</p>
  
  <pre><code>import requests
  import sys
  import json
  with open('big.xml', encoding="utf-8") as f:
      xml_string = f.read()
      print(sys.getsizeof(xml_string) // 1024 // 1024)
      # 283
      gid = "FOO"
      json_data = json.dumps({"file_content": xml_string, "self_id": gid})
      print(sys.getsizeof(json_data) // 1024 // 1024)
      # 305
      result_json = requests.post("http://my_server:8080/api", data=json_data, headers={"Content-Type": "application/json"})
  </code></pre>
  
  <p>As you can see, xml files can be quite big, around 300 MB in this example.</p>
  
  <p>My flask app, to simplify, looks like this:</p>
  
  <pre><code>from flask import Flask, request, jsonify
  from memory_profiler import profile
  
  app = Flask(__name__)
  
  @app.route('/api', methods=['POST'])
  @profile
  def api():
      input_data = request.get_json()
      output_data = {"id": "FOO"}
      response = jsonify(output_data)
      return response
  
  if __name__ == "__main__":
      app.run(host='0.0.0.0', port=8080, debug=True)
  </code></pre>
  
  <p>During the request being posted, the memory usage of the flask app spikes to <strong>~2.8 GB</strong>. Memory profiling in the code above is nowhere close to these numbers:</p>
  
  <pre><code>Line #    Mem usage    Increment   Line Contents
  ================================================
       6     27.8 MiB     27.8 MiB   @app.route('/api', methods=['POST'])
       7                             @profile
       8                             def api():
       9    617.3 MiB    589.5 MiB       input_data = request.get_json(request.data)
      10    617.3 MiB      0.0 MiB       output_data = {"id": "FOO"}
      11    617.3 MiB      0.0 MiB       response = jsonify(output_data)
      12    617.3 MiB      0.0 MiB       return response
  </code></pre>
  
  <p>What am I missing? What causes this big memory spike and how to deal with it?</p>