PythonMemory Management101 DeepinginGarbagecollector Jos Manuel - - PowerPoint PPT Presentation

python memory management 101
SMART_READER_LITE
LIVE PREVIEW

PythonMemory Management101 DeepinginGarbagecollector Jos Manuel - - PowerPoint PPT Presentation

PythonMemory Management101 DeepinginGarbagecollector Jos Manuel Ortega @jmortegac Aboutme @jmortegac http://jmortega.github.io https://www.linkedin.com/in/jmortega1/ Agenda


slide-1
SLIDE 1

José Manuel Ortega @jmortegac

PythonMemory Management101

DeepinginGarbagecollector

slide-2
SLIDE 2

Aboutme

  • @jmortegac
  • http://jmortega.github.io
  • https://www.linkedin.com/in/jmortega1/
slide-3
SLIDE 3

Agenda

  • Introductiontomemorymanagement
  • Garbagecollectorandreference

countingwithpython

  • Reviewthegcmoduleforconfiguring

thepythongarbagecollector

  • Bestpracticesformemorymanagment
slide-4
SLIDE 4

Introductiontomemorymanagement

  • Memorymanagementistheprocessof

efficientlyallocating,de-allocating,and coordinatingmemorysothatallthe differentprocessesrunsmoothlyandcan

  • ptimallyaccessdifferentsystem

resources.

slide-5
SLIDE 5

PythonObjectsinMemory

slide-6
SLIDE 6

PythonMemoryManager

slide-7
SLIDE 7

Heapallocation

slide-8
SLIDE 8

def main(): x=300 print(id(x)) w=fun(x) print(id(w)) def sqr(x): print (id(x)) z=x*x print(id(z)) return z if __name__ == "__main__": main()

Heapallocation

slide-9
SLIDE 9

PythonObjectsinMemory

  • EachvariableinPythonactsasanobject
  • Pythonisadynamicallytypedlanguage

whichmeansthatwedonotneedto declaretypesforvariables.

slide-10
SLIDE 10

PythonObjectsinMemory

slide-11
SLIDE 11

PythonObjectsinMemory

slide-12
SLIDE 12

PythonObjectsinMemory

slide-13
SLIDE 13

PythonObjectsinMemory

slide-14
SLIDE 14

id()method

slide-15
SLIDE 15

id()method

slide-16
SLIDE 16

id()method

slide-17
SLIDE 17

isOperator

slide-18
SLIDE 18

Referencecounting

  • Pythonmanagesobjectsbyusingreference

counting

  • Referencecountingworksbycountingthe

numberoftimesanobjectisreferencedby

  • therobjectsintheapplication.
  • Whenreferencestoanobjectareremoved,

thereferencecountforanobjectis decremented.

slide-19
SLIDE 19
  • Areferenceisacontainerobjectpointingat

anotherobject.

  • Referencecountingisasimpletechnique

inwhichobjectsareallocatedwhenthereis referencetotheminaprogram

Referencecounting

slide-20
SLIDE 20
  • whenreferencecountincreases?

○ x=1 ○ def(x): ○ list.append(x)

Referencecounting

slide-21
SLIDE 21

Referencecounting

slide-22
SLIDE 22

Referencecounting

slide-23
SLIDE 23

Referencecounting

slide-24
SLIDE 24
  • Easytoimplement
  • Objectsareimmediatelydeletedwhen

referencecounteris0 ✗Notthread-safe ✗Doesn’tdetectcyclicreferences ✗spaceoverhead-referencecountis storedforeveryobject

Referencecounting

slide-25
SLIDE 25

Garbagecollector(GC)module

slide-26
SLIDE 26
  • ReferenceCounting+GenerationalGC
  • RefCountreacheszero,immediate

deletion

  • Deletedobjectswithcyclicreferences

aredeletedwithTracingGC

PythonGarbagecollector

slide-27
SLIDE 27

Garbagecollector(GC)referencecycle

slide-28
SLIDE 28

>>> def ref_cycle(): ... list = [1, 2, 3, 4] ... list.append(list) ... return list

Garbagecollector(GC)referencecycle

slide-29
SLIDE 29

Garbage collector(GC) reference cycle

import gc for i in range(8): ref_cycle() n = gc.collect() print("Number of unreachable objects collected by GC:", n) print("Uncollectable garbage:", gc.garbage) print("Number of unreachable objects collected by GC:", gc.collect())

slide-30
SLIDE 30

Garbage collector(GC) reference cycle

slide-31
SLIDE 31

PythonObjectGraphs https://mg.pov.lt/objgraph/

import objgraph x = "hello" y = [x, [x], list(x), dict(x=x)]

  • bjgraph.show_refs([y],

filename='sample-graph.png')

slide-32
SLIDE 32

Bestpracticesformemorymanagement

  • Usinggc.collect()carefully

print("Collecting...") n = gc.collect() print("Number of unreachable objects collected:", n) print("Uncollectable garbage:", gc.garbage)

slide-33
SLIDE 33

Garbage collector(GC) methods

slide-34
SLIDE 34
  • Usingwithcontextmanagerforworking

withfiles with open('data.txt', 'r') as file: data = ','.join(line.strip() for line in file)

Bestpracticesformemorymanagement

slide-35
SLIDE 35
  • AvoidListSlicingwith[:]

list= [1,2,3,4] list[1:3] list[slice(1,3)]

Bestpracticesformemorymanagement

slide-36
SLIDE 36

Bestpracticesformemorymanagment

  • StringConcatenation

string= “hello” string+= “world” wordList = ("hello", "world") string = " ".join(wordList)

slide-37
SLIDE 37
  • UseIteratorsandGenerators

Bestpracticesformemorymanagement

slide-38
SLIDE 38
  • https://stackabuse.com/basics-of-memory-management-in-p

ython/

  • https://realpython.com/python-memory-management
  • https://rushter.com/blog/python-garbage-collector
  • https://pythonchb.github.io/PythonTopics/weak_references.

html

References

slide-39
SLIDE 39

https://www.youtube.com/c/JoseManuelOrtegadev