3.5. Memory Management

So far you have seen a large number of Python code samples and may have noticed a few interesting details about variables and memory management. Highlighting some of the more conspicuous ones, we have:

  • Variables not declared ahead of time

  • Variable types are not declared

  • No memory management on programmers' part

  • Variable names can be “recycled”

  • del statement allows for explicit “deallocation”

3.5.1. Variable Declarations (or Lack Thereof)

In most compiled languages, variables must be declared before they are used. In fact, C is even more restrictive: Variables have to be declared at the beginning of a code block and before any statements are given. Other languages, like C++ and Java, allow “on-the-fly” declarations i.e., those which occur in the middle of a body of code—but these name and type declarations are still required before the variables can be used. In Python, there are no explicit variable declarations. Variables are “declared” on first assignment. Like most languages, however, variables cannot be accessed until they are (created and) assigned:

>>> a
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: a

Once a variable has been assigned, you can access it by using its name:

>>> x = 4
>>> y = 'this is a string'
>>> x
4
>>> y
'this is a string'

3.5.2. Dynamic Typing

Another observation, in addition to lack of variable declaration, is the lack of type specification. In Python, the type and memory space for an object are determined and allocated at run-time. Although code is byte-compiled, Python is still an interpreted language. On creation, that is, on assignment, the interpreter creates an object whose type is dictated by the syntax that is used for the operand on the right-hand side of an assignment. After the object is created, a reference to that object is assigned to the variable on the left-hand side of the assignment.

3.5.3. Memory Allocation

As responsible programmers, we are aware that when allocating memory space for variables, we are borrowing system resources, and eventually, we will have to return that which we borrowed back to the system. Happily, not only do we not have to explicitly allocate the memory, we don't have to deallocate it either. That is memory management made easy. Well, okay, perhaps it had something to do with the decision that Python should simply be a tool for the application writer to and shouldn't have to worry about lower-level, operating system or machine-oriented tasks.

3.5.4. Garbage Collection

Memory that is no longer being used is reclaimed by the system using a mechanism known as garbage collection. Python's garbage collector will automatically deallocate a data object once is it no longer needed, all without requiring any management on the programmer's part. How does Python decide when an object is “no longer needed?” By keeping track of the number of references to objects. This is called reference counting.

3.5.5. Reference Counting

To keep track of memory that has been allocated, Python does something quite similar to card-counting, a popular scheme used in casino gaming. When an object is created, a reference is made to that object. An internal tracking variable, a reference counter, keeps track of how many references are being made to each object. The reference count for an object is initially set to one (1) when an object is created and (its reference) assigned.

New references to objects, also called aliases, occur when additional variables are assigned to the same object, passed as arguments to invoke other bodies of code such as functions, methods, or class instantiation, or assigned as members of a sequence or mapping.

# initialize string object, set reference count to 1
foo1 = 'foobar'

# increment reference count by assigning another variable
foo2 = foo1  # create an alias

# increment ref count again temporarily by calling function
check_val(foo1)

In the function call above, the reference count is set to one on creation, incremented when an alias is created, and incremented again when the object participated in a function call. The reference count is decremented when the function call has completed; and once again if foo2 is removed from the namespace. The reference count goes to zero and the object deallocated when foo1 goes out of scope. (See Section 11.8 for more information on variable scope.)

3.5.6. del Statement

The del statement removes a single reference to an object, and its syntax is:

							del obj1[, obj2[, … objN… ]]

For example, executing del foo2 in the example above has two results:

(1) removes name foo2 from namespace

(2) lowers reference count to object 'foobar' (by one)

Further still, executing del foo1 will remove the final reference to the 'foobar' object, decrementing the reference counter to zero and causing the object to become “inaccessible” or “unreachable.” It is at this point that the object becomes a candidate for garbage collection. Note that any tracing or debugging facility may keep additional references to an object, delaying or postponing that object from being garbage-collected.

3.5.7. Decrementing Reference Count

You already noticed that when the del statement was executed, an object was not really “deleted,” rather just a reference to it. Likewise, you can “lose” the reference to an object by reassigning it to another object.

foo1 = 'foobar'          # create original string
foo1 = 'a new string'    # 'foobar' "lost" and reclaimed

The preceding example shows how all references to an object can occur with reassigning a variable. The most common case utilizes neither reassignment nor calling the del statement.

Exiting from the current scope means that when a piece of code such as a function or method has completed, all the objects created within that scope are destroyed (unless passed back as a return object), such as our example above when foo1 is given as an argument to the check_val() function. The reference count for foo1 is incremented on the call and decremented when the function completed.

We present below a reference count decrementing summary. The reference count for an object is decremented when a variable referencing the object…

  • Is named explicitly in a del statement

  • Is (re)assigned to another object

  • Goes out-of-scope

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset