Python Guppy Recent Memory Usage of a Program














































Python Guppy Recent Memory Usage of a Program




Recent Memory Usage of a Program

In this Python article, we will check the recent memory usage of the program using guppy module. guppy module is used to provide the Heapy subsystem, which supports object and heap memory sizing, profiling and debugging. It assists us in directly displaying the RAM memory usage in a well-formatted tabular manner. 

guppy is not an in-built module, meaning it does not come preinstalled with Python 3 IDLE (Integrated Development Learning Environment, available at python.org, the official website for Python). 'guppy' was initially implemented with Python v2.x, and still guppy supports Python 2. So, they've released 'guppy3' to extend their support to Python v3. It can be installed using any package manager like pip. You'll need to install "guppy" for Python 2, and "guppy3" for Python 3, from your python package manager.

For accessing heap memory, we will use guppy.hpy().heap() method. Here, guppy.hpy() method is the top level interface to Heapy. Heap is a hierarchical data structure, is a location in memory where memory may get allocated randomly.

For better understanding, check the source codes given below:

from guppy import hpy
import networkx as nx

h
= hpy()
L
=[1,2,3]

h
.heap()

> Partition of a set of 89849 objects. Total size = 12530016 bytes.
> Index Count % Size % Cumulative % Kind (class / dict of class)
> 0 40337 45 3638400 29 3638400 29 str
> 1 21681 24 1874216 15 5512616 44 tuple
> 2 1435 2 1262344 10 6774960 54 dict (no owner)

If you want to plot the total usage as "time" progresses in a simulation,
you can call hpy() function along with size to get  complete details about the memory 
usage of the code.
This is the basic bit of code you can do:
x = h.heap()
x
.size

returns the total size. For example:

from guppy import hpy
import networkx as nx

h
= hpy()

num_nodes
= 1000
num_edges
= 5000
G
= nx.gnm_random_graph(num_nodes, num_edges)

x
= h.heap()
print(x.size)

#prints

#19820968
#which is consistent with the Total size reported by
print(x)
# Partition of a set of 118369 objects. Total size = 19820904 bytes.
# Index Count % Size % Cumulative % Kind (class / dict of class)
# 0 51057 43 6905536 35 6905536 35 str
# 1 7726 7 3683536 19 10589072 53 dict (no owner)
# 2 28416 24 2523064 13 13112136 66 tuple
# 3 516 0 1641312 8 14753448 74 dict of module
# 4 7446 6 953088 5 15706536 79 types.CodeType
# 5 6950 6 834000 4 16540536 83 function
# 6 584 0 628160 3 17168696 87 dict of type
# 7 584 0 523144 3 17691840 89 type
# 8 169 0 461696 2 18153536 92 unicode
# 9 174 0 181584 1 18335120 93 dict of class
# <235 more rows. Type e.g. '_.more' to view.>


Happy Pythoning..!!



More Articles of Aditi Kothiyal:

Name Views Likes
Python AdaBoost Mathematics Behind AdaBoost 421 1
Python PyCaret How to optimize the probability threshold % in binary classification 2069 0
Python K-means Predicting Iris Flower Species 1322 2
Python PyCaret How to ignore certain columns for model building 2624 0
Python PyCaret Experiment Logging 680 0
Python PyWin32 Open a File in Excel 941 0
Python Guppy GSL Introduction 219 2
Python Usage of Guppy With Example 1101 2
Python Naive Bayes Tutorial 552 2
Python Guppy Recent Memory Usage of a Program 893 2
Introduction to AdaBoost 290 1
Python AdaBoost Implementation of AdaBoost 513 1
Python AdaBoost Advantages and Disadvantages of AdaBoost 3713 1
Python K-Means Clustering Applications 332 2
Python Random Forest Algorithm Decision Trees 440 0
Python K-means Clustering PREDICTING IRIS FLOWER SPECIES 457 1
Python Random Forest Algorithm Bootstrap 476 0
Python PyCaret Util Functions 441 0
Python K-means Music Genre Classification 1763 1
Python PyWin Attach an Excel file to Outlook 1541 0
Python Guppy GSL Document and Test Example 248 2
Python Random Forest Algorithm Bagging 386 0
Python AdaBoost An Example of How AdaBoost Works 279 1
Python PyWin32 Getting Started PyWin32 603 0
Python Naive Bayes in Machine Learning 375 2
Python PyCaret How to improve results from hyperparameter tuning by increasing "n_iter" 1724 0
Python PyCaret Getting Started with PyCaret 2.0 356 1
Python PyCaret Tune Model 1325 1
Python PyCaret Create your own AutoML software 321 0
Python PyCaret Intoduction to PyCaret 296 1
Python PyCaret Compare Models 2696 1
Python PyWin Copying Data into Excel 1153 0
Python Guppy Error: expected function body after function declarator 413 2
Python Coding Random forest classifier using xgBoost 247 0
Python PyCaret How to tune "n parameter" in unsupervised experiments 659 0
Python PyCaret How to programmatically define data types in the setup function 1403 0
Python PyCaret Ensemble Model 805 1
Python Random forest algorithm Introduction 227 0
Python k-means Clustering Example 337 1
Python PyCaret Plot Model 1243 1
Python Hamming Distance 715 0
Python Understanding Random forest algorithm 311 0
Python PyCaret Sort a Dictionary by Keys 244 0
Python Coding Random forest classifier using sklearn 340 0
Python Guppy Introduction 368 2
Python How to use Guppy/Heapy for tracking down Memory Usage 1069 2
Python AdaBoost Summary and Conclusion 232 1
Python PyCaret Create Model 365 1
Python k -means Clusturing Introduction 325 2
Python k-means Clustering With Example 348 2

Comments