mariadb oom-killer on CentOS8 in EC2 t2.micro instance

The OOM killer is a kernel function that operates on behalf of the process currently allocating memory. This can be the same process that is chosen to be killed to free memory.

The selection rules generally choose the process with the largest mapping of anonymous memory (i.e. discounting memory mapped files) to physical pages (i.e. discounting memory that is swapped out or has never been written to).

It is usually possible to tune databases to use less memory at the expense of performance, and I'd also expect the default tuning to assume that the database server is a dedicated machine and should devote all of its resources to this single task, which is not what you are doing here.

So there are multiple avenues here:

  1. run a separate VM that is dedicated to the database
  2. tune database settings to use less memory
  3. use a larger VM

Databases are usually good at adapting to however much memory you have available and use it for caches that are known to the query optimizer and can be part of cost calculation (as opposed to OS-level disk caches, which are unpredictable), so either limiting that to some value that leaves enough space for the other software, or making sure no other software needs space solves that nicely.

If that is still insufficient, you will need a larger VM.

Adding swap space is not helpful, because it makes the access times of the database internal caches unpredictable, so the database will start creating suboptimal query plans as it assumes that data is cached and an access to the in-memory copy will be faster than reloading the data from disk -- but swap space on a VM where you pay for I/O is not a good idea either way.