<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1870732089876948&amp;ev=PageView&amp;noscript=1">

Profiling Memory Usage with Guppy

Guppy for Profiling Memory | CloudbakersSource: http://insights.dice.com/2012/07/13/the-rise-of-in-memory-databases/

Services like Google Cloud Platform and Amazon Web Services make it easy to develop and deploy your own applications, without the hassle and cost of maintaining your own infrastructure in-house. This convenience, however, can be a massive crutch for developers that don’t analyze their code for inefficiencies that require too many resources.

Memory usage is an important performance metric that can be easily overlooked when writing an application that retrieves large amounts of data from a database or third-party service. Let’s say your app is programmed to accept a user-specified query, retrieve some records from a database, and then iterate over each result. If the user specifies a large query, then your app will have to load a lot of records from the database (which is designed to store lots data) to the server’s memory (which is not designed to store lots of data). Using too much memory will drive up the cost of hosting your application, or worse, cause the app to crash entirely. Before you rewrite your code to optimize memory usage, however, it’s important to measure its current memory usage and locate the lines of code that need improvement.

Guppy is a command-line tool that makes it easy to profile your Django app’s memory usage. You can install the module with only a couple steps and see its benefits demonstrated in the examples below. Each view builds a list of full names using the first_name and last_name attributes of each row in the “People” table.

def all_rows(request):
   full_names = []
   for person in People.objects.all():
       full_name = '%s %s'%(person.first_name, person.last_name)
       full_names.append(full_name)
   return HttpResponse()

def each_row(request):
   full_names = []
   for person_id in People.objects.all().values_list('id', flat=True):
       person = People.objects.get(pk=person_id)
       full_name = '%s %s'%(person.first_name, person.last_name)
       full_names.append(full_name)
   return HttpResponse()

You can execute each view after running the setref() command in Guppy’s interactive console, and then analyze the memory usage with the heap() command. Although the each_row view makes more database queries, it loads a smaller total amount of data into memory. This is useful for situations where less memory usage is worth a longer execution time.

Try using Guppy with your own application and see where it might be using too much memory. A few simple code changes may significantly reduce the hosting cost and improve its overall functionality. And for more development tips, check out our post on the Cloudbakers Development Stack.

New Call-to-action

Originally published on February 22, 2016

If you enjoyed this post, please consider sharing

Cloud colaboration
New Call-to-action

Subscribe to
Crumbs From the Cloud

Topics

Posts by Topic

see all