What I did to save Python memory

background

Launch multiple processes that take several days on a small server with 4 CPU memory and 4G When I was calculating with 100% CPU x 4, I ran out of memory in a few hours. CPU usage has dropped sharply. Apparently it was running out of memory.

What was happening

Increased processing time Nitomonai, increased process memory usage → Memory exhaustion → Memory swap occurs → Memory access speed deteriorates significantly → Memory waiting CPU usage is 1% or less → It never ends (significant deterioration in performance)

Measure 0. Solved with money

In most cases, increasing the number of servers or improving the performance of the server will solve the problem. I didn't select it because it was a personal PJ, but when I switched to a server with about 32G of memory I wonder if that was the only solution.

Countermeasure 1. Vertical division of processing

Memory management of python is fully automatic, leaving it to the basic VM. The only solution to a memory leak is to kill the process Idempotent processing was given, and the part where processing of 8 types of categories was performed with one command was divided.

Before improvement

class Category(Enum):
    A = 1
    B = 2
    C = 3

for category in Category:
  benchmark(category)

After improvement

category = manage.get_category_by_priority()
benchmark(category)

Countermeasure 2. Introduced supervisor

The code for improvement 1 had to be executed in 8 steps, so if the process stopped It was necessary to start up again. It is convenient to use supervisor in such a case

shell


easy_install supervisor
echo_supervisord_conf > /etc/supervisord.conf
supervisord
supervisord status
alias sc='supervisorctl'
sc restart
sc reread
sc stop all
sc status
sc restart all

Countermeasure 3. I got my hands on manual GC

I'm not familiar with python's GC, so it may have side effects. So far, the memory leak has been resolved and it is stable. There is a possibility of black magic, so I can't recommend it very much.

If you use a lot of class cache, memory leaks occur frequently in python2 series. Will python3 solve the problem of steadily increasing memory consumption?

class Category(Enum):
    A = 1
    B = 2
    C = 3

for category in Category:
  benchmark(category)

def benchmark(category):
  bulk = []
  tmp_data = Tmp.get_all()
  for _tmp in tmp_data:
    bulk.append(calc(_tmp))
  DBTable.bulk_create(bulk)  #Bulk!

  #Memory release
  import gc
  del tmp_data
  del bulk
  gc.collect()

Reference: gc — Garbage collector interface http://docs.python.jp/2/library/gc.html

Recommended Posts

What I did to save Python memory
What I did when updating from Python 2.6 to 2.7
What I did to welcome the Python2 EOL with confidence
What I did with a Python array
What I was addicted to Python autorun
What I did when I wanted to make Python faster -Numba edition-
[At Coder] What I did to reach the green rank in Python
What I did to ssh to the VPS Ubuntu environment
What I learned in Python
What I did to get started with Linux commands
What I was addicted to when using Python tornado
What I did to speed up the string search task
What I was addicted to when migrating Processing users to Python
I tried to touch Python (installation)
H29.2.27 ~ 3.5 Summary of what I did
I want to debug with Python
Did not change from Python 2 to 3
What I did to output application logs on GAE Flexible Environment.
A note on what you did to use Flycheck with Python
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
I made Othello to teach Python3 to children (4)
I installed Python 3.5.1 to study machine learning
I made Othello to teach Python3 to children (2)
I tried to implement PLSA in Python 2
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
Function to save images by date [python3]
I want to analyze logs with Python
I made Othello to teach Python3 to children (5)
I tried to implement ADALINE in Python
What I got from Python Boot Camp
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
[Python] I tried to calculate TF-IDF steadily
I did Python data analysis training remotely
I tried to touch Python (basic syntax)
I made Othello to teach Python3 to children (3)
I wanted to solve ABC172 with Python
What I did to keep track of the humidity and temperature of the archive
After all, what should I use to do type comparisons in Python?
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
Updated to Python 2.7.9
I want to do Dunnett's test in Python
I wanted to solve NOMURA Contest 2020 with Python
I want to memoize including Python keyword arguments
I was able to recurse in Python: lambda
I tried to save the data with discord
I want to create a window in Python
How to implement shared memory in Python (mmap.mmap)
[Python] I want to manage 7DaysToDie from Discord! 1/3
I started python
I want to make a game with Python
I tried Python! ] I graduated today from "What is Python! Python!"!
I tried to explain what a Python generator is for as easily as possible.
I wanted to install Python 3.4.3 with Homebrew + pyenv
I wrote "Introduction to Effect Verification" in Python