2

In python3 is there a simple way to see how much memory is used when loading a module? (not while running its content such as functions or methods, which may load data and so on).

# Memory used before, in bytes
import mymodule
# Memory used after, in bytes
# Delta memory = memory used before - memory used after

(E.g. these 3 comment lines of extra code to insert would be what I call "simple").

By using the spyder IDE for example, I can see in the "File explorer" tab on the top right, the size of the file (i.e. size on disk) which contains my module, but I think it's not the size that is taken into memory after Python has actually loaded its contents, with the many imports I need in there.

And in the "Memory and Swap History" part of the "System Monitor" (Ubuntu 18.04) I can see a little bump while effectively loading my module in python (it may get bigger as the module grows of course) and which is probably the amount I'm searching for:

Memory usage

My uses would mainly be inside the Spyder IDE, any jupyter-notebook or directly into a python console.

swiss_knight
  • 5,787
  • 8
  • 50
  • 92

1 Answers1

0

Probably after 3 years its not relevant anymore, but I would link you to this question: Total memory used by Python process?

Since you mention you were using Ubuntu 18.04, you can do this with the standard library module resource

resource.getrusage(resource.RUSAGE_SELF).ru_maxrss # peak memory usage (platform dependent)

Your code would look like this:

import resource
m1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
import mymodule
m2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
delta = m2-m1

I tested it with Python console, not sure if jupyter spawns another process. Also note the discussion in the linked question that the returned value is platform dependent.

codingmonkey87
  • 186
  • 1
  • 4