Full Swap memory partition

xml

Active member
Last night I found my 16 Ram VPS and MariaDB10.3, the SWAP memory partition was almost 100% and Ram memory usage 68% (57% of ram is for Mysql alone), the load average was normal and I am on XF2.2.12

1673011582595.webp

1673011674085.webp


Am I in critical situation? if yes what should I do?
 
Last night I found my 16 Ram VPS and MariaDB10.3, the SWAP memory partition was almost 100% and Ram memory usage 68% (57% of ram is for Mysql alone), the load average was normal and I am on XF2.2.12

View attachment 279518

View attachment 279519


Am I in critical situation? if yes what should I do?
this issue happens when you have high amount of traffic running for long time

service mariadb restart
then clear cache
sync; echo 1 > /proc/sys/vm/drop_caches
sync; echo 2 > /proc/sys/vm/drop_caches
sync; echo 3 > /proc/sys/vm/drop_caches
 
something needs to be tweaked out in MySQL settings

This is /etc/my.cnf file, any suggestions?
Code:
!includedir /etc/my.cnf.d

[mysqld]
log-error=/var/lib/mysql/error.log.err
performance-schema=0
tmpdir=/home/mysqltmp

#default-storage-engine=MyISAM
max_connections=250
thread_cache_size=256

table_open_cache=8000
table_definition_cache=8000
tmp_table_size=1024M
max_heap_table_size=1024M

query_cache_limit=4M
query_cache_size=128M
query_cache_type=1

key_buffer_size=2048M
join_buffer_size=1M
read_buffer_size=16M

connect_timeout=10
max_connect_errors=10
max_allowed_packet=256M
myisam_sort_buffer_size=64M

open_files_limit=10000
innodb_buffer_pool_size=10G
innodb_file_per_table=1
 
Before I start optimizing MySQL I need to know if it will solve the SWAP memory issue
It could help use less memory, ya. If you are just trying to remove the swap from being used currently, you could turn swap off and back on like so:

Code:
swapoff -a && swapon -a

If it's more of an issue where the server is hitting the swap always because it just doesn't have enough memory, you probably need more memory in the server (can't magically not use swap if it's running low on memory).

You honestly can't have enough physical memory in servers, so if you are starting to run up against memory limits, some optimizations might help a little bit, but if your database/site is growing, you probably do need to start at least thinking about a server with more memory (optimizations aren't going to solve everything, just buy time). I'm actually getting ready to add an ADDITIONAL 512GB (yes, GB) RAM to all my servers soon, so... (no, they aren't just for XenForo)
 
Last edited:
It could help use less memory, ya. If you are just trying to remove the swap from being used currently, you could turn swap off and back on like so:

Code:
swapoff -a && swapon -a

If it's more of an issue where the server is hitting the swap always because it just doesn't have enough memory, you probably need more memory in the server (can't magically not use swap if it's running low on memory).

You honestly can't have enough physical memory in servers, so if you are starting to run up against memory limits, some optimizations might help a little bit, but if your database/site is growing, you probably do need to start at least thinking about a server with more memory (optimizations aren't going to solve everything, just buy time). I'm actually getting ready to add an ADDITIONAL 512GB (yes, GB) RAM to all my servers soon, so... (no, they aren't just for XenForo)

When my VPS swap hits 100% the Ram memory usage was 68% which means my VPS wasn't running out of memory there was still 33% available and also the VPS average load was very normal and my XF forums was running normally
 
You honestly can't have enough physical memory in servers, so if you are starting to run up against memory limits, some optimizations might help a little bit, but if your database/site is growing, you probably do need to start at least thinking about a server with more memory
Indeed. When everything is optimized (especially Mysql and PHP) and you still run into swap usage, add more ram.
If a vps with more ram is too expensive, check out dedicated servers. (But only if you are confident you can run your own server safely!)

You can rent dedicated server for cheap nowadays, for as little as $50 a month.
Check out Hetzner for a good and cheap hoster.
 
When my VPS swap hits 100% the Ram memory usage was 68% which means my VPS wasn't running out of memory there was still 33% available and also the VPS average load was very normal and my XF forums was running normally
It's not quite as simple as "there was enough memory". The operating system is what decides when to use (or not use swap). So for example if you had 33% of total memory free but that 33% was split into a zillion tiny chunks of non-sequential memory, it might choose to use a sequential swap vs. 5,000 non-sequential small bits of RAM. So the "total free" doesn't tell the full story. More specifically, Linux has memory "pages"... and a full "page" of memory is what it cares about when dealing with swap.

/proc/meminfo has RAM info more inline with what the kernel cares about (for example from one of my servers):

Code:
twin1:~ # cat /proc/meminfo
MemTotal:       528340872 kB
MemFree:        182607612 kB
MemAvailable:   188004596 kB
Buffers:          813296 kB
Cached:          7640236 kB
SwapCached:            0 kB
Active:         58281384 kB
Inactive:        9571752 kB
Active(anon):   55649252 kB
Inactive(anon):  4512464 kB
Active(file):    2632132 kB
Inactive(file):  5059288 kB
Unevictable:    275092064 kB
Mlocked:        275092064 kB
SwapTotal:       2096124 kB
SwapFree:        2096124 kB
Dirty:              1228 kB
Writeback:             0 kB
AnonPages:      334477188 kB
Mapped:          3092008 kB
Shmem:            758084 kB
Slab:            1242784 kB
SReclaimable:    1021136 kB
SUnreclaim:       221648 kB
KernelStack:       30736 kB
PageTables:       681996 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    266266560 kB
Committed_AS:   326975972 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:  332660736 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:     1820756 kB
DirectMap2M:    265531392 kB
DirectMap1G:    271581184 kB

One thing I do is run this command on my servers weekly to help optimize/reorganize RAM (might be helpful for you):

Code:
free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free

But again... if you are running into swap issues (for whatever reason), it's time to think about more memory (not just optimization).
 
I forgot to mention that even though your current RAM usage is 67%, that may not have been the case when the swap needed to be used. So if your server was overloaded at one point, swap kicked in, and then it went back to normal, the swap will persist normally. Swap usage is a good indicator that the server was out of memory at some point, but not necessarily now. If you want to reset swap usage back to 0, the command I gave you previously is what will do that (turning swap off and back on):

Code:
swapoff -a && swapon -a

Again... normally Linux doesn't reset swap back to 0 even when it's recovered from a resource overload event.
 
So for example if you had 33% of total memory free but that 33% was split into a zillion tiny chunks of non-sequential memory, it might choose to use a sequential swap vs. 5,000 non-sequential small bits of RAM.
That explain what I was missing. I will optimize the MySQL but should I also consider to increase the swap partition to be equal to Ram size? or the reset swap usage is the better option?
 
That explain what I was missing. I will optimize the MySQL but should I also consider to increase the swap partition to be equal to Ram size? or the reset swap usage is the better option?
It's never good when a server needs to use swap (I wouldn't let your server use more... swap should just be reserved for emergency use). Again, you need more RAM, not more swap for those high usage situations.
 
  • Like
Reactions: xml
Top Bottom