"Python" Memory analysis _list object Memory footprint Analysis

Source: Internet
Author: User

"Python" Memory Analysis _ _list and array in memory growth mode

After the list declaration structure is broadly divided into 3 parts, the variable name--list object (structural data + pointer array)--list content , where the ID represents the position of the list object,

V reference variable name, v[:] refers to the list object, which is also set up for the other Python sequence structure, which is supported by the following demonstration available IDs,

A=b, A and B point to the same list object

a=b[:], A's list object and B's List object point to the same list content

Q1: Whether the element storage address is contiguous

The memory address of the contents (structure 3) stored by the list object is tested first.

In [1]: a=[1,2,3, ' A ', ' B ', ' C ', ' de ', [4,5]]in [2]: ID (a) out[2]: 139717112576840In [3]: For I in a:   ...:     print (ID (i) )   ...:     1397172387699201397172387699521397172387699841397172398341921397172400774801397172405238881397171952811041 39717112078024In [4]: For I in a[6]:   ...:     print (ID (i))   ...:     139717240220952139717240202048In [5]: for I in a[7]:   ...:     print (ID (i))   ...:     139717238770016139717238770048

Then look at the relative address,

In [6]: For I in a:   ...:     print (ID (i) -139717238769920)   ...:     03264106427213075601753968-43488816-126691896in [7]: For I in a[6]:   ...:     print (ID (i)-139717238769920)   ...:     14510321432128In [8]: For I in a[7]: ...:     print (ID (i) -139717238769920)   ...:     96128

Visible, for the list object, its element content is not necessarily linear storage, but due to memory allocation problem, there will be a linear storage illusion, when the element appears in the container or relative to the previous element type changes, the memory space will be no longer contiguous .

Q2:list object address and element address are contiguous

In fact, Q1 has answered this question, after all, the element address itself is not continuous, but we still tested a bit,

In []: ID (a[0])-id (a) out[22]: 126193080

Very far, and we analyze the source, the list object body is an array of pointers, that is, the ID (a) refers to the location of the body is a pointer to the location of the array of pointers, of course, as well as the auxiliary object header information (python in a few common "black box" list).

Q3:list object (without element) memory consumption analysis
in [+]: sys.getsizeof ([120In, ' A ', ' B ', ' C ', ' de ']) out[16]: [+]: sys.getsizeof ([All-in-all, ' a ', ' B ', ' C ']) out[17]: 112In [+]: sys.getsizeof ([104, ' A ', ' B ')) out[18]:

Visible, list each object occupies 8 bytes of 32 bits of space, we look at the slices,

In []: Sys.getsizeof (A[:3]) out[20]: 88In [96In]: sys.getsizeof (A[:4]) out[21]: [max]: sys.getsizeof (A[3:4]) out[23]: 72In []: sys.getsizeof (A[3:5]) out[24]: 80

The Slice object is also 8 bytes per element, but the slice is also a list object, even if it is cut from the middle (without cutting the head), it also contains the storage footprint of the header information.

Note

1, [0] and [: 1] Different

in [+]: a[0]out[30]: 1In []: a[:1]out[31]: [1]

2. Empty list occupies space

in [+]: sys.getsizeof ([]) out[32]: 64

"Python" Memory analysis _list object Memory footprint Analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.