简体   繁体   中英

Freeing up buffer space after use in Python?

So I'm using Google Cloud data Lab and I use the %%storage read command to read in a large file (2,000,000 rows) Into the text variable and then I have to process it into a pandas dataframe using BytesIO eg df_new=pd.read_csv(BytesIO(text))

So now I don't need the text Variable or its contents around, (all further processing is done on df_new , how can I delete it ( text ) and free up memory (I sure don't need two copies of a 2 million record dataset hanging around...)

Use del followed by forced garbage collection.

import gc

# Remove text variable
del text
# Force gc collection - this not actually necessary, but may be useful.
gc.collect()

Note that you may not see process size decreasing and memory returning to OS, depending on memory allocator used (depends on OS, core libraries used and python compilation options).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM