title: Cython: How to move large objects without copying them?
body: <p>I use Cython to wrap C++ code and expose it to Python for interactive work. My problem is that I need to read large graphs (several gigabytes) from file and they end up twice in the memory. Can anyone help me diagnose and solve this problem? </p>
  
  <p>My Cython wrapper for the graph class looks like this:</p>
  
  <pre><code>cdef extern from "../src/graph/Graph.h":
      cdef cppclass _Graph "Graph":
          _Graph() except +
          _Graph(count) except +
          count numberOfNodes() except +
          count numberOfEdges() except +
  
  
  cdef class Graph:
      """An undirected, optionally weighted graph"""
      cdef _Graph _this
  
      def __cinit__(self, n=None):
          if n is not None:
              self._this = _Graph(n)
  
      # any _thisect which appears as a return type needs to implement setThis
      cdef setThis(self, _Graph other):
          #del self._this
          self._this = other
          return self
  
      def numberOfNodes(self):
          return self._this.numberOfNodes()
  
      def numberOfEdges(self):
          return self._this.numberOfEdges()
  </code></pre>
  
  <p>If a Python Graph needs to be returned, it needs to be created empty and then the <code>setThis</code> method is used to set the native <code>_Graph</code> instance. This happens, for example, when a <code>Graph</code> is read from file. This is the job of this class:</p>
  
  <pre><code>cdef extern from "../src/io/METISGraphReader.h":
      cdef cppclass _METISGraphReader "METISGraphReader":
          _METISGraphReader() except +
          _Graph read(string path) except +
  
  cdef class METISGraphReader:
      """ Reads the METIS adjacency file format [1]
          [1]: http://people.sc.fsu.edu/~jburkardt/data/metis_graph/metis_graph.html
      """
      cdef _METISGraphReader _this
  
      def read(self, path):
          pathbytes = path.encode("utf-8") # string needs to be converted to bytes, which are coerced to std::string
          return Graph(0).setThis(self._this.read(pathbytes))
  </code></pre>
  
  <p>Interactive usage looks like this:</p>
  
  <pre><code> &gt;&gt;&gt; G = graphio.METISGraphReader().read("giant.metis.graph")
  </code></pre>
  
  <p>After the reading from file is done and X GB memory are used, there is a phase where obviously copying happens, and after that 2X GB memory are used. The entire memory is freed when <code>del G</code> is called.</p>
  
  <p>Where is my error which leads to the graph being copied and existing twice in memory?</p>