0

I looking for easy way to automatic removing duplicated of immutable types for save memory (Use the same object by reference).

For example: code 1 and code 2 doing the same work, but code 1 using about 200 Mb, and code 2 only about 6 Mb.

There any solution for this? Memory managers or something else?

Code 1 (about 200 Mb):

a = []
for _ in range(100000):
    a.append('my immutable data' * 100)

Code 2 (about 6 Mb):

a = []
my_string = 'my immutable data' * 100
for _ in range(100000):
    a.append(my_string)

I can use sys.intern() for string. But I also interested in other immutable types like tuple.

One of solutions:

class InternElements:
    def __init__(self):
        self.__elements_dict = {}

    def intern(self, element: Any):
        try:
            return self.__elements_dict[element]
        except KeyError:
            self.__elements_dict[element] = element
            return element

    def extend(self, elements: Iterable[Any]):
        for element in elements:
            self.intern(element)
ADR
  • 1,255
  • 9
  • 20
  • 4
    I am not sure how your code example is relevant to the question (if I get it). In your example 1, you are creating different strings (hence creation of 100000 string) and in the another you are referencing same string. It is obvious that there will be difference in the memory usage – Moinuddin Quadri Feb 06 '17 at 19:53
  • Thanks! But what about other types? For example `tuple`? – ADR Feb 06 '17 at 19:59

0 Answers0