#NEWLINE# def HandlePyHeaps( unused_request , response ) : #NEWLINE# #INDENT# 'Renders /pyheaps page with Python object memory usage.' #NEWLINE# response . headers . add_header ( 'Content-Type' , 'text/plain' ) #NEWLINE# response . start_response ( 200 ) #NEWLINE# count = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# mem_used = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for obj in gc . get_objects ( ) : #NEWLINE# #INDENT# count [ type ( obj ) ] = ( count . get ( type ( obj ) , 0 ) + 1 ) #NEWLINE# mem_used [ type ( obj ) ] = ( mem_used . get ( type ( obj ) , 0 ) + sys . getsizeof ( obj ) ) #NEWLINE# #UNINDENT# response . out . write ( 'Python objects (total bytes, count, type):\n\n' ) #NEWLINE# for ( total_size , type_ ) in sorted ( ( ( v , k ) for ( k , v ) in mem_used . iteritems ( ) ) , reverse = True ) : #NEWLINE# #INDENT# response . out . write ( ( '%12s %12s  %s\n' % ( total_size , count [ k ] , type_ ) ) ) #NEWLINE# #UNINDENT# response . end_response ( )