3/29/2023 0 Comments Treeview winform![]() The instance can only contain the attributes defined in _slots_, which affects the flexibility of writing programs. So is it really necessary to use slots? Using _slots_ also has side effects: each inherited subclass must redefine _slots_ The _slots_ declaration contains several instance variables, and reserves just enough space for each instance to hold each variable in this way, Python will no longer use dict, thus saving space. One workaround is to define a _slots_ attribute in the new-style class. This problem becomes especially prominent when a large number of instances need to be created. However, for some small classes that know that there are several fixed attributes before "compilation", this dict is a bit of a waste of memory. This is generally not bad, and is so flexible that you can set new properties at will in the program. It can be seen that the memory usage has been significantly reducedīy default, instances of both Python's new-style and classic classes have a dict to store the instance's attributes. You can see the memory usage, class is a little less than dict, but it is far from enough.įrom the memory footprint distribution of the class, we can find that by eliminating _dict_ and _weakref_, the size of the class instance in RAM can be significantly reduced, and we can achieve this by using _slots_. > print(sys.getsizeof(ob), sys.getsizeof(ob._dict_)) ![]() Starting with Python 3.3, keys are stored in shared memory, reducing the size of the instance tracker in RAM. Let's take a look at the memory usage in this case:įor _weakref_ (weak reference), you can check this document, some self.xxx things are stored in the _dict_ of the object. The data structure of class is very different from Dict. Use the same requirements using class: class Point: Imagine that if there are a lot of such data to be stored, it will take up more memory.įor programmers who like object-oriented programming, they prefer to package data in a class. The simple three integers take up a lot of memory. > ob = Ĭheck the memory size occupied by the following ob object: > print(sys.getsizeof(ob)) It is very simple to use Python's built-in data structure Dict to realize the requirements of the above example. Let's take a simple scene, using Python to store a three-dimensional coordinate data, x, y, z. Below I will give a few ways to optimize the memory occupied by Python.Įxplanation: The following code runs on Python3. Large and small scenes are more prone to problems. When the memory usage reaches a certain value, the program may be terminated by the operating system, especially when limiting the memory used by the program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |