Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:42:42+00:00 2026-06-15T23:42:42+00:00

Query in Python interpreter: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32

  • 0

Query in Python interpreter:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> k = [i for i in xrange(9999999)]
>>> import sys
>>> sys.getsizeof(k)/1024/1024
38
>>>

And here – see how much it takes from RAM:

Memory usage after statement del k:

And after gc.collect():

Why list of integers with expected size of 38Mb takes 160Mb?

UPD: This part of question was answered (almost immediately and multiple times :))

Okay – here is another riddle:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys

>>> str = 'abcdefg'
>>> sys.getsizeof(str)
28
>>> k = []
>>> for i in xrange(9999999):
...     k.append(str)
...
>>> sys.getsizeof(str)*9999999/1024/1024
267

How much do you think it will consume now?


(source: i.imm.io)

Size of str is 28, vs 12 in past example. So, expected memory usage is 267Mb – even more then with integers. But it takes only ~40Mb!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:42:43+00:00Added an answer on June 15, 2026 at 11:42 pm

    sys.getsizeof() is not very useful because it accounts often for only a part of what you expect. In this case, it accounts for the list, but not all integer objects that are in the list. The list takes roughly 4 bytes per item. The integer objects take another 12 bytes each. For example, if you try this:

    k = [42] * 9999999
    print sys.getsizeof(k)
    

    you’ll see that the list still takes 4 bytes per item, i.e. around 40MB, but because all items are pointers to the same integer object 42, the total memory usage is not much more than 40MB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to define a XPath type query for nested python dictionaries.
I perform this kind of query in my Python code to conn = rdbms.connect(instance=_INSTANCE_NAME,
I want my python script to simultaneously accept POST variables and query string variables
I'm new to python and have hit a problem with an SQL query I'm
On Google App Engine to query the data store with Python, one can use
MySqlDb is a fantastic Python module -- but one part is incredibly annoying. Query
I have a bytearray object in Python 2.7 that I got from a query
I want to execute postgres query in python.The table name has to be passed
I use a simple SQL query in Python to grab records from a SQLite
I would like to construct a sqlite3 database query in python, then write it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.