ARTIFACTORY: How to troubleshoot High Swap memory issue Artifactory java process
In some scenarios, we might observe high usage of the OS swap by the Artifactory process which would cause performance issues with the Artifactory. In this article, we will discuss what steps to be taken to address the high swap memory issue.
What is Swap memory?Swap memory, also known as page file on systems, is a reserved space on a computer's hard drive or SSD that is used as virtual memory when the physical RAM (Random Access Memory) is fully utilized. When the RAM is full, the operating system moves inactive or less frequently used data from the RAM to the swap space to free up physical memory for active processes. While swap memory helps prevent system crashes due to insufficient RAM, accessing data from the swap space is slower than from RAM, as it involves reading and writing to the storage device.
Whenever you observe high usage of swap memory it is always recommended to check the following factors.
1. Identifying Memory-Intensive Processes:
Identify and optimize processes that consume a significant amount of memory.
2. Adjusting Swappiness:
Tune the swappiness value to control how often the system swaps to disk. Lowering it may reduce swap usage.
3. Adding Physical RAM:
If possible, adding more physical RAM can alleviate the need for excessive swapping.
Investigating Memory Leaks:
4. Check for memory leaks in applications, as they can lead to increased memory usage over time.
5. Monitoring System Resources:
Use tools like “top” or “htop” to monitor system resource usage and identify potential issues.
Below are the tools/examples using which we can identify high swap memory issue.
1. Output of the free -h command.
E.g:
[root@ system]# free -h total used free shared buff/cache available Mem: 31Gi 19Gi 1.1Gi 126Mi 10Gi 11Gi Swap: 4.0Gi 3.9Gi 127Mi
Out of 31 GB of RAM, 19 GB worth is used by the applications, 10 GB worth of buffers and cache , and 126 MB of worth of used by the shared memory e.g files on a tmpfs.In other words, about 29.1 GiB of your RAM is effectively in use which is about 94 per cent of RAM being used and if you look at the swap memory usage by Java it is around 3.9 GB. Only the available free memory is 1.1GB which is not sufficient to handle the Java usage, hence, OS( kernel) moved the request to use the swap usage. Hence, we might need to tune the JVM memory allocated for Artifactory and also think of increasing the memory allocated for the server where Artifactory is hosted. However, keep in mind that when increasing the JVM memory allocation, make sure you leave at least 30% of the total RAM to the OS and other services.
E.g:
shared: extraJavaOpts: "-Xms4g -Xmx12g"
2. Linux commands such as top and also the below shell script to list the top 10 processes with the highest swap usage.
for file in /proc/*/status ; do echo "pid $(echo $file | awk -F"/" {'print $3'}): $(awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file)"; done | sort -rnk 4 | head -10
E.g output:
pid 10526: java 465008 kB pid 1367534 splunkd 19312 kb pid 12490: node 14300 kB pid 1234: tuned 12848 kB pid 2849305: puppet 8100 kB pid 1207: polkitd 6672 kB pid 10341: (sd-pam) 4044 kB pid 1367558: splunkd 3680 kB pid 898: systemd-journal 3624 kB pid 2927464: agentid-service 2988 kB
In some cases, it's not only with Artifactory, we might have additional applications in the server where Artifactory is hosted that might be causing the memory usage.
3. Collect the histogram using jmap (which is a command line tool in Java used for generating memory-related information about Java processes)
jmap -histo <Artifactory_prcess_id)
and see how much memory is utilized by Artifactory Java, and if it is within the JVM memory configured for the Artifactory, then Artifactory is not the issue here. We might need to further check if any other application is contributing to the high utilization of swap memory. Also, taking take heap histograms would help us find if there are any objects that are constantly increasing in size (possible memory leak).
4. Taking Heap Dump:
$ /opt/jfrog/artifactory/app/third-party/java/bin/jmap -dump:live,format=b,file=/opt/jfrog/artifactory/var/log/hdump.hprof <JAVA_PID>
Heap dumps are valuable in diagnosing and troubleshooting memory-related issues. If we observe memory-related problems, the heap dump is the first place to look when troubleshooting.
5. Tuning the swappiness value Linux.
Swappiness is a Linux kernel parameter that controls the tendency of the system to move unused memory pages to the swap space (a dedicated area on disk used as virtual memory) instead of keeping them in RAM. It is a value between 0 and 100.
A low swappiness value (e.g., 0) means the kernel will try to avoid swapping as much as possible, keeping more data in RAM.
A high swappiness value (e.g., 100) means the kernel will be more aggressive in moving memory pages to swap, which can be useful in certain scenarios where you want to free up RAM quickly.
Tuning swappiness is often done to optimize system performance based on the specific workload and requirements of the system. If you would like to move away from swap memory, as explained in the Swappiness Wikipedia, you could set this value in a virtual file /proc/sys/vm/swappiness. The mentioned value will be deciding how the swap space should be used, below are the values with their intents.
vm.swappiness = 0 # Swap is disabled. In earlier versions, this meant that the kernel would swap only to avoid an out of memory condition, but in later versions this is achieved by setting to 1. vm.swappiness = 1 # Kernel version 3.5 and over, as well as kernel version 2.6.32-303 and over: Minimum amount of swapping without disabling it entirely. vm.swappiness = 10 # This value is sometimes recommended to improve performance when sufficient memory exists in a system, this value *10* could be considered for the performance being expected. vm.swappiness = 60 # The default value. vm.swappiness = 100 # The kernel will swap aggressively.
Although it actually depends upon the need too, if the physical memory available is sufficient, there may not be a great need for swap space, in Layman's terms there won't be a need to change the default value of 60.
ConclusionThe usage of the swap memory would not really pose any risk, especially with RAM not being maxed out. If you wish to reduce the use of swap memory, you can reduce the swapiness value to a lower value. But it is normal for Linux systems to use some swap even if there is still RAM free.
The Linux kernel will move to swap memory pages that are very seldom used. Swap space usage becomes an issue only when there is not enough RAM available, and the kernel is forced to continuously move memory pages to swap and back to RAM, just to keep applications running also when the OS layer switches the memory usage to the swap memory option despite the memory being available to be served from the actual allocated actual RAM. Then, we need find out if there are any issues with the swap usage using the above-mentioned methods.