In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
 
            
                     
                
2025-11-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how python uses _ _ slots__ to reduce memory expenses, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.
Use _ _ slots__ to reduce memory overhead
Have you noticed that your Python application takes up a lot of resources, especially memory? One trick is to use the _ _ slots__ class variable to reduce memory expenditure to some extent.
Import sysclassFileSystem (object): def _ init__ (self,files,folders,devices): self.files= filesself.folders= foldersself.devices= devicesprint (sys.getsizeof (FileSystem)) classFileSystem1 (object): _ _ slots__= ['files','folders','devices'] def _ init__ (self,files,folders,devices): self.files= filesself.folders= foldersself.devices= devicesprint (sys.getsizeof (FileSystem1)) # In Python 3.531-> 1016room2-> 888
Obviously, you can see from the results that there are savings in memory usage, but you should only use _ _ slots__ when the memory overhead of a class is unnecessarily large. Use it only after performance analysis of the application, otherwise you just make the code difficult to change without real benefits.
[translator's note: the above result in my win10 python2.7 is:
# In Python 2.7win10#1- > 896 win10#1--> 1016
Therefore, this comparison method is not so convincing. _ _ slots__ is mainly used to limit the attribute information of objects. In addition, it may cost less when there are many objects generated. For more information, please see the official documentation of python:
The slots declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because dict is not created for each instance. ]
Thank you for reading this article carefully. I hope the article "how python uses _ _ slots__ to reduce memory expenses" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about

The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r


A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from

Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





 
             
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.