Is your Amazon RDS DB instance using swap memory when you already have sufficient memory? Well, the Support Engineers at Bobcares have a solution for this snag.
If you are running an Amazon RDS DB where your DB instance is using more swap memory even though you have enough memory, it is time to call in the experts. The Support Team at Bobcares resolved this problem for one of our customers recently and is happy to share the solution here.
Why is Amazon RDS DB instance using swap memory?
Amazon Elastic Compute Cloud instances rely on swap memory when the system winds up requiring more memory than allocated. So when you find your Amazon RDS DB instance using swap memory even though you have allocated sufficient memory can be confusing. Fortunately, our Support Team is well-versed in coming up with solutions for issues like these.
The Amazon RDS DB instances need pages in the RAM only in situations where the pages are accessed. For instance, while executing queries.
The process involves bringing in other pages into the RAM by previously executed queries. This flushes swap space in case the pages are not used recently. In other words, the OS rends to swap older pages rather than holding on to pages. This ensures that plenty of free RAM is available for upcoming queries.
Furthermore, swap memory is not cleared frequently in Linux as it requires extra overhead to reallocated the cleared swap. So if swap space is used by the RDS DB instance, the SwapUsage metrics will not return to zero.
Additionally, swap memory is also utilized when you use HugePages on RDS for PostgreSQL or HugePages supported by Amazon RDS Oracle.
How to fix Amazon RDS DB instance using swap memory?
In order to resolve this particular issue, we will first take a look at the DB performance metrics based on the application load. This involves taking a look at SwapUsage and FreeableMemory Amazon CloudWatch metrics. Moreover, it also helps you to get a grasp of the overall memory usage pattern of the RDS DB instance.
Verify if there is a decrease in the FreeableMemory metric while there is an increase in the SwapUsage metric at the same time.
This is a clear indication of pressure on the RDS DB instance’s memory. In case the FreeableMemory remains low consistently, change the RDS DB instance size to one with more memory.
You can monitor SwapUsage by turning on Enhanced Monitoring. It collects statistics at the host level while CloudWatch collects data every sixty seconds from the hypervisor level.
Moreover, you can turn on Performance Insights to identify the SQL as well as wait events responsible for excessive memory or swap on the RDS DB instance.
Amazon RDS MySQL using Swap memory
If you notice freeable memory is low, execute SHOW FULL PROCESSLIST. This will review the threads running on the database.
The process ID displayed by Enhanced Monitoring will not match the one from the output of SHOW FULL PROCESSLIST.
In order to see the correct process ID, change the DB parameter group associated with the database to enable Performance_Schema.
Moreover, since it is static, you need to reboot the RDS DB instance. Once the memory has reached the required usage, follow these steps:
- First, sort the process IDS in the Enhanced Monitoring page to identify the ones consuming maximum CPU.
- Then, execute the following query as master user:
select * from performance_schema.threads where THREAD_OS_ID in (ID shown in the Enhanced Monitoring window)\G
For instance: execute the following query if the maximum memory is consumed by Thread_OS_Id 10373 and 1432:
select * from performance_schema.threads where THREAD_OS_ID in (10373, 1432)\G
- Then, access the PROCESSLIST_ID of the output from the executed in the previous step. This will match the process ID from SHOW FULL PROCESSLIST.
Amazon RDS PostgreSQL using Swap memory
In this scenario, our Support Engineers first identify the process responsible for consuming a high amount of memory. The process ID from the Enhanced Monitoring process list is mapped to the exact query by running:
select * from pg_stat_activity where pid=(the PID of your process);
Then, we will configure the queries to consume less memory.
Amazon RDS SQL Server using Swap memory
Enhanced Monitoring comes in handy to identify the thread ID that is consuming more memory. The thread ID also goes by the name, Kernal process ID by SQL Servers.
You can access this information by running the following query:
select * from sys.sysprocesses where kpid = '<Value of Thread ID from Enhanced Monitoring>' ;
Run the following command for instance if you get hold of the server process ID as 68:
Amazon RDS Oracle using Swap memory
Here, we will use the OS process ID from Enhanced Monitoring, to identify which process is using the most memory. Then, we will execute this query to attain the process address:
select ADDR from v$process where SPID=OS_PID;
In turn, the process address identifies the session within the database with the following query:
select sid,serial#,username, status from v$session where PADDR='<ADDR from above query>';
[Need some help with Server Management? We are here to lend a hand 24/7.]
To conclude, we learned how to resolve the issue when an Amazon RDS DB instance uses swap memory even if there is sufficient memory available. The talented Support Team at Bobcares showed us the root cause behind the issue and how to resolve it by tracking Freeablememory and SwapUsage.