Wednesday, 15 April 2015

Python: Memory Management Optimization Inconsistencies? -



Python: Memory Management Optimization Inconsistencies? -

i'm quite confused python objects' allocations in memory. seems allocation of predefined types doesn't behave consistently. here product of cogitations on issue:

a = none b = none print( a, b a) # outputs true, 1 single instance of none = 'a' b = 'a' print( a, b a) # outputs true, 1 single instance of string = 2 b = 2 print( a, b a) # outputs true, 1 single instance of int = 2.5 b = 2.5 print( a, b a) # outputs true, 1 single instance of float # python command line 'b a' returns false = 'a b' b = 'a b' print( a, b a) # outputs true, 1 single instances of same string # python command line 'b a' returns false = () b = () print( a, b a) # outputs true, 1 single instance of () = {} b = {} print( a, b a) # outputs false, 2 different instances of same empty {} = [] b = [] print( a, b a) # outputs false, 2 different instances of same []

the id homecoming values a , b show is operator working 'memory usage optimization' algorithm seems working inconsistently.

are lastly 2 print outputs , python command line interpreter behavior revealing implementation bugs, or python supposed behave way?

i ran tests in opensuse 13.1 env. python 2.7.6 , python 3.3.5 (default, mar 27 2014, 17:16:46) [gcc] on linux.

apart output differences between command line , program, reason of type of optimization? think quite optimistic assume programs save more 10% of memory on average unless consider special cases should managed straight programmer.

does behavior help minimize memory fragmentation?

the difference here of objects mutable, , immutable.

it safe optimise out assignments e.g. string literals, because 2 references same immutable object won't cause problems. can't alter object in-place, alter mean new object, separate old one.

however, mutable types lists, end in problem if set a = b. mutable objects can changed in-place, in list example, appended a end in b , vice versa.

the behaviour in interpreter different (except little integers, "interned"), these optimisations not beingness carried out:

>>> = "a b" >>> b = "a b" >>> b false

python python-2.7 memory-management python-3.x

No comments:

Post a Comment