This is an old revision of the document!
02. [20p] Memory usage
Open ex03.py and take a look at the code.
By looking at the source code, what is the difference between the methods do_append and do_allocate?
Use vmstat to monitor the memory usage while performing the following experiments. In the main method, call:
The do_append method on it's own (see Experiment 1).
The do_allocate method on it's own (see Experiment 2).
Both methods as shown in the Experiment 3 area in the code.
Both methods as shown in the Experiment 4 area in the code.
Offer an interpretation for the obtained results.
The reason that
do_append and
do_allocate do not differ too much is because
[None] * size does not allocate
None size times.
In stead, all elements of the array reference the same
None object. Try this experiment in a python shell:
>>> v = [ [None] * 3 ] * 3
>>> print v
>>> v[0][0] = 1
>>> print v
Notice that all the [None, None, None] arrays change, even if we only modified one.