How to memory dump an object?

Steven D'Aprano steve at pearwood.info
Sat May 21 00:56:10 EDT 2016


On 2016年5月21日 11:05 am, jfong at ms4.hinet.net wrote:
> Is there any tools which can do the memory dump of an object so I can view
> their content or implementation? 

No standard tool. There may be third-party tools, but they would be
implementation-specific.
> For example, 
>>>>> s1 = '\x80abc'
>>>> b1 = b'\x80abc'
>> What are exactly stored in memory for each of them? 

That will depend on the version and implementation of the Python interpreter
you are looking at.
Strings especially are implemented in many different ways. Before Python
3.3, CPython used two different implementations, which you choose when
compiling the interpreter from source:
- "narrow builds" use two-bytes per character, and have difficulty 
 with Unicode codepoints after U+FFFF;
- "wide builds" use four-bytes per character, and can cope with 
 the entire Unicode range up to U+10FFFF;
- I think that Jython uses the Java string implementation;
- and that IronPython uses the .Net string implementation.
Starting at Python 3.3, the CPython implementation will dynamically choose a
one-byte, two-byte or four-byte per character implementation, as needed.
> Is their content really the same?

The first is the Unicode string with four code points:
<control-0080>
LATIN SMALL LETTER A
LATIN SMALL LETTER B
LATIN SMALL LETTER C
the other contains a byte-string with four bytes:
\x80
\x61 (ASCII 'a')
\x62 (ASCII 'b')
\x63 (ASCII 'c')
So in *some* sense they are the same content, but only in the sense that
this list:
[128, 97, 98, 99]
*also* has the same content.
Instead of a low-level memory dump, which isn't actually terribly useful
(for many objects, such as lists, all it would show is a bunch of
pointers), there are other high-level introspection tools available in the
`inspect` module. You can also use:
sys.getsizeof
to see how much memory is used by an object.
-- 
Steven


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /