title: Am I effectively using memory_usage in Python and why the strange behavior?
body: <p>I could use a little help flushing out some python code that takes advantage of &quot;memory_usage&quot; from &quot;memory_profiler&quot;.  The code syntactically seems to work, however I don't believe I'm getting accurate readings from my memory_usage call and I think I might be doing something wrong since this portion of the code exhibits some strange behavior.</p>
  <ol>
  <li><p><strong>Program Overview</strong> - The code is a menu-driven program that allows the user to specify the size of an array, populate that array with randomly generated numbers, then apply various sorting algorithms to measure both execution time and memory utilization.  To measure time, I am using &quot;timeit&quot; and to measure memory I'm attempting to use &quot;memory_usage&quot;.</p>
  </li>
  <li><p><strong>Program Structure</strong> - The body of the program is primarily a menu that makes functions calls to each sorting or counting algo when the user selects that algo in the menu. Once the initial selection is made the user is prompted to specify the desired size of array they want to build with <code>tempArray = [randint(1, 10000) for i in range(size)]</code>.  Once the array is built then the user can select whether they want to measure time of execution or memory utilization.</p>
  </li>
  <li><p><strong>The Issue</strong> - Whenever the program calls &quot;memory_usage&quot; via the below command it takes forever (even when using &quot;Counter&quot; to find the &quot;.most_common&quot; occurrences in an array of 15 numbers).  It also creates some strange behavior by preventing me from ending the program (using a 'break' from the 'while True' loop running the main menu).  Eventually it will allow me to exit after several escape sequences while finally printing the &quot;MAX memory usage...&quot; on exit but the results don't seem to be accurate (ie.. 27MB to run this function?).</p>
  </li>
  </ol>
  <p>Here is the main code in question...</p>
  <pre><code>        if (structure == 1):
              tempArray = [randint(1, 10000) for i in range(size)]
              # Use the collections module counter class to count elements of the array
              print(most_frequent(tempArray))
              # To turn on memory analysis and check spacial usage
              mem_usage = memory_usage((most_frequent, (tempArray,)))
              print (&quot;\nMAX memory usage in MB: &quot;,max(mem_usage))
  </code></pre>
  <p>If it helps this is the short function returning the most frequently occurring elements of the array</p>
  <pre><code># Function to determine entry frequency of an array or heap
  def most_frequent(data):
      elements_count = Counter(data)
      # Return frequency counts
      return elements_count.most_common(10)
  </code></pre>
  <p>Happy to share any additional code that would be helpful.</p>
  <p>Maybe my novice knowledge of memory_profilier is showing or maybe there is a better way to do this.  I'm open to any feedback, thoughts, or suggestions.  Appreciate any help, tnx!</p>