0

It seems mysqld allocates too much memory as buff/cache:

free -m
              total        used        free      shared  buff/cache available
Mem:            990         448          96          36         445         326
Swap:           511         511           0

Now I cannot start httpd service because it "Failed to fork: Cannot allocate memory".

I wonder why this happens? Why the 445M buff/cache cannot be reclaimed and used for httpd?

William
  • 69
  • 2
  • 10

2 Answers2

1

The column "buff/cache" reports the sum of memory in use for buffers and cache.

Buffers cannot be reclaimed because they are needed by the kernel to operate.

Cache could be reclaimed, as witnessed by the "available" column. This would however require writing the current content of the reclaimed memory to swap if the cache page in question is dirty. But your swap is full, as witnessed by the 0 in the "free" column of the "swap" line.

In sum, memory allocation fails because your swap is full, or in other words, because your virtual memory is exhausted.

Tilman Schmidt
  • 3,778
  • 10
  • 23
  • Must the cache content be written to swap before reclaimed? I think the cache content can be discarded at any time because it can be read from disk later. – William Mar 07 '21 at 15:41
  • In this article:https://www.linuxatemyram.com/, it says: "No, disk caching only borrows the ram that applications don't currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping." – William Mar 07 '21 at 15:52
  • It must if the cache page in question is dirty. I edited my answer to specify that. Your quote from the linuxatemyram.com article doesn't contradict me. Applications will try to reclaim memory from cache, which may require free swap space in the case of dirty cache pages. But it's the reclaimed cache page that's swapped out, not the application. – Tilman Schmidt Mar 07 '21 at 18:52
  • I'm still confused about the memory/swap usage. Why did not the system use the available memory but used up the swap space? Did the following scenario happen: program1 used up all memory, then program2 had to use swap, later, program1 exited leaving 326M available memory. If so, why the system does not swap in the data from disk to the available memory? I'm still not clear about the concept of dirty cache. Is all virtual memory a program allocates belong to "used memory", or just the part of written memory belongs to "used" while other parts are "cache"? – William Mar 10 '21 at 15:39
  • That's the most probable scenario, but you won't be able to find out for sure after the fact, and it wouldn't buy you anything anyway. As to "why", swapping and cache usage are a pretty complex balance trying to optimize performance, and there are even a few knobs (such as "swappiness") to influence it. (Although in most cases you end up making things worse by fiddling with them.) But basically, it rarely makes any sense to swap in data just because there's reclaimable memory without knowing if or when it might be actually used. – Tilman Schmidt Mar 10 '21 at 23:07
  • @TilmanSchmidt why cannot os just flush dirty cache into its origin disk file instead of swap? – scottxiao Apr 06 '22 at 13:23
  • Of course it can. But it may decide that doing so would be bad performance-wise. – Tilman Schmidt Apr 06 '22 at 17:31
1

1GB is a very small machine for running MySQL in. Aside from the items already discussed, look in the my.cnf (or wherever MySQL's config file is). Look for or set (in [mysqld] section), the following:

max_connections = 10
innod_buffer_pool_size = 150M
key_buffer_size = 10M
table_open_cache = 50

Lower your values to these; it may help get things started.

What version of MySQL (or MariaDB) are you using? What other apps do you have on the same machine?

Rick James
  • 2,058
  • 5
  • 11