{"id":39,"date":"2026-04-20T23:25:50","date_gmt":"2026-04-20T15:25:50","guid":{"rendered":"https:\/\/blog.uika.cn\/?p=39"},"modified":"2026-04-20T23:25:52","modified_gmt":"2026-04-20T15:25:52","slug":"linux-%e5%b0%8f%e5%86%85%e5%ad%98-vps-%e9%85%8d%e7%bd%ae-swap-%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97%ef%bc%88btrfs-%e8%b8%a9%e5%9d%91%e8%ae%b0%e5%bd%95%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.uika.cn\/?p=39","title":{"rendered":"Linux \u5c0f\u5185\u5b58 VPS \u914d\u7f6e Swap \u5b8c\u6574\u6307\u5357\uff08btrfs \u8e29\u5751\u8bb0\u5f55\uff09"},"content":{"rendered":"\n<div class=\"wp-block-sakurairo-notice-block shortcodestyle warning\"><i class=\"fa-solid fa-triangle-exclamation\"><\/i><span><strong>\u672c\u6587\u5185\u5bb9\u57fa\u4e8e\u4f5c\u8005\u672c\u4eba\u5728\u5b9e\u9645\u90e8\u7f72\u8fc7\u7a0b\u4e2d\u4e0e AI\uff08Claude \/ ChatGPT \u7b49\uff09\u7684\u591a\u8f6e\u8ba8\u8bba\u4e0e\u8bf7\u6559\u6574\u7406\u800c\u6210\uff0c\u5e76\u5728\u90e8\u7f72\u5b8c\u6210\u3001\u9a8c\u8bc1\u53ef\u884c\u540e\u7531 AI \u534f\u52a9\u5f52\u7eb3\u6210\u6587\u3002<br>\u6587\u4e2d\u6240\u6709\u547d\u4ee4\u3001\u914d\u7f6e\u4e0e\u6d41\u7a0b\u5747\u7ecf\u8fc7\u4f5c\u8005\u771f\u5b9e\u73af\u5883\u9a8c\u8bc1\uff0c\u4f46\u56e0\u670d\u52a1\u5668\u73af\u5883\u3001\u8f6f\u4ef6\u7248\u672c\u3001\u7f51\u7edc\u6761\u4ef6\u7b49\u5dee\u5f02\uff0c\u8bfb\u8005\u5728\u590d\u73b0\u65f6\u4ecd\u53ef\u80fd\u9047\u5230\u672c\u6587\u672a\u8986\u76d6\u7684\u95ee\u9898\u3002\u8bf7\u7ed3\u5408\u81ea\u8eab\u5b9e\u9645\u60c5\u51b5\u8c28\u614e\u64cd\u4f5c\uff0c\u5e76\u81ea\u884c\u627f\u62c5\u76f8\u5173\u98ce\u9669\u3002<\/strong><\/span><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u672c\u6587\u8bb0\u5f55\u5728\u4e00\u53f0 1C 0.5G \u914d\u7f6e\u7684 KVM VPS \u4e0a\u914d\u7f6e Swap \u7684\u5b8c\u6574\u8fc7\u7a0b\uff0c\u5305\u542b btrfs \u6587\u4ef6\u7cfb\u7edf\u4e0b\u7684\u8e29\u5751\u89e3\u51b3\u65b9\u6848\uff0c\u4ee5\u53ca\u9488\u5bf9\u4ee3\u7406\/\u7aef\u53e3\u8f6c\u53d1\u573a\u666f\u7684\u5185\u6838\u53c2\u6570\u4f18\u5316\u5efa\u8bae\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u80cc\u666f\">\u80cc\u666f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u65b0\u5f00\u4e86\u4e00\u53f0 1 \u6838 512MB \u5185\u5b58\u7684 KVM \u5c0f\u9e21\uff0c\u4e3b\u8981\u7528\u9014\u662f\u505a\u4ee3\u7406\u548c\u7aef\u53e3\u8f6c\u53d1\u3002\u8fd9\u79cd\u4f4e\u5185\u5b58\u673a\u5668\u4e3a\u4e86\u9632\u6b62 OOM\uff0c\u914d\u7f6e Swap \u662f\u5fc5\u8981\u64cd\u4f5c\u3002\u672c\u4ee5\u4e3a\u662f\u4e94\u5206\u949f\u641e\u5b9a\u7684\u4e8b\uff0c\u7ed3\u679c\u5728 btrfs \u6587\u4ef6\u7cfb\u7edf\u4e0a\u7ffb\u8f66\u4e86\uff0c\u8bb0\u5f55\u4e00\u4e0b\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4e00\u3001swap-\u5927\u5c0f\u600e\u4e48\u9009\">\u4e00\u3001Swap \u5927\u5c0f\u600e\u4e48\u9009<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u7684\u7ecf\u9a8c\u6cd5\u5219\u662f <code>swap = RAM \u00d7 2<\/code>\uff0c\u4f46\u66f4\u5b9e\u7528\u7684\u662f\u6309\u7528\u9014\u6765\u5b9a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7528\u9014<\/th><th>\u63a8\u8350 Swap \u5927\u5c0f<\/th><\/tr><\/thead><tbody><tr><td>\u8f7b\u91cf web \/ \u4ee3\u7406 \/ bot<\/td><td>512MB ~ 1GB<\/td><\/tr><tr><td>Node.js \/ Python \u5e94\u7528<\/td><td>1GB<\/td><\/tr><tr><td>\u5076\u5c14\u7f16\u8bd1\u4ee3\u7801<\/td><td>1 ~ 2GB<\/td><\/tr><tr><td>\u6570\u636e\u5e93\uff08MySQL\/Postgres\uff09<\/td><td>1 ~ 2GB<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>0.5G \u5185\u5b58\u673a\u5668\u63a8\u8350 1GB Swap<\/strong>\uff0c\u591f\u7528\u4e14\u4e0d\u4f1a\u592a\u5360\u78c1\u76d8\u7a7a\u95f4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4e8c\u3001\u6807\u51c6\u914d\u7f6e\u6d41\u7a0b\">\u4e8c\u3001\u6807\u51c6\u914d\u7f6e\u6d41\u7a0b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u901a\u7528\u6b65\u9aa4\">\u901a\u7528\u6b65\u9aa4<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u521b\u5efa 1GB \u7684 swap \u6587\u4ef6\nsudo fallocate -l 1G \/swapfile\n\n# 2. \u8bbe\u7f6e\u6743\u9650\uff08\u5fc5\u987b\u53ea\u80fd\u88ab root \u8bfb\u5199\uff09\nsudo chmod 600 \/swapfile\n\n# 3. \u683c\u5f0f\u5316\u4e3a swap\nsudo mkswap \/swapfile\n\n# 4. \u7acb\u5373\u542f\u7528\nsudo swapon \/swapfile\n\n# 5. \u6dfb\u52a0\u5230 \/etc\/fstab \u5f00\u673a\u81ea\u52a8\u6302\u8f7d\uff08\u5148\u5907\u4efd\u539f\u6587\u4ef6\uff09\nsudo cp \/etc\/fstab \/etc\/fstab.bak\necho '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab\n\n# 6. \u9a8c\u8bc1\u662f\u5426\u751f\u6548\nsudo swapon --show\nfree -h\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u6267\u884c\u5230 <code>swapon<\/code> \u8fd9\u4e00\u6b65\u5c31\u80fd\u770b\u5230 Swap \u751f\u6548\u3002\u4f46\u8fd9\u6b21\u7ffb\u8f66\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4e09\u3001\u8e29\u5751\uff1aswapon-failed-invalid-argument\">\u4e09\u3001\u8e29\u5751\uff1aswapon failed: Invalid argument<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u95ee\u9898\u73b0\u8c61\">\u95ee\u9898\u73b0\u8c61<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>root@server:~# sudo swapon \/swapfile\nswapon: \/swapfile: swapon failed: Invalid argument\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mkswap<\/code> \u80fd\u6b63\u5e38\u6267\u884c\uff0c\u4f46 <code>swapon<\/code> \u76f4\u63a5\u62d2\u7edd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6392\u67e5\u6b65\u9aa4\">\u6392\u67e5\u6b65\u9aa4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7b2c\u4e00\u6b65\uff1a\u786e\u8ba4\u865a\u62df\u5316\u7c7b\u578b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemd-detect-virt\n# \u8f93\u51fa\uff1akvm\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">KVM \u662f\u5b8c\u6574\u865a\u62df\u5316\uff0c\u652f\u6301 swap\uff0c\u6392\u9664 OpenVZ\/LXC \u5bb9\u5668\u9650\u5236\u7684\u53ef\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7b2c\u4e8c\u6b65\uff1a\u6362\u7528 dd \u521b\u5efa\u6587\u4ef6<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u5019 <code>fallocate<\/code> \u5728\u67d0\u4e9b\u6587\u4ef6\u7cfb\u7edf\u4e0a\u4f1a\u521b\u5efa\u7a00\u758f\u6587\u4ef6\uff0c\u5bfc\u81f4 <code>mkswap<\/code> \u80fd\u8fc7\u4f46 <code>swapon<\/code> \u62d2\u7edd\u3002\u4f46\u8fd9\u6b21\u6362\u6210 <code>dd<\/code> \u4f9d\u7136\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7b2c\u4e09\u6b65\uff1a\u67e5\u770b\u5185\u6838\u65e5\u5fd7<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -T \/\n# Filesystem  Type   ...\n# \/dev\/vda3   btrfs  ...\n\nsudo dmesg | tail -20\n# &#91;141925.195702] BTRFS warning (device vda3): swapfile must not be copy-on-write\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u771f\u76f8\u5927\u767d<\/strong>\uff1a\u6839\u5206\u533a\u662f btrfs\uff0c\u800c btrfs \u7684 swapfile \u4e0d\u80fd\u5e26 copy-on-write (CoW) \u5c5e\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e3a\u4ec0\u4e48-btrfs-\u6709\u8fd9\u4e2a\u9650\u5236\">\u4e3a\u4ec0\u4e48 btrfs \u6709\u8fd9\u4e2a\u9650\u5236<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">btrfs \u9ed8\u8ba4\u6240\u6709\u6587\u4ef6\u90fd\u542f\u7528 CoW\uff0c\u5373\u4fee\u6539\u6570\u636e\u65f6\u4f1a\u5199\u5230\u65b0\u7684\u5757\u4f4d\u7f6e\uff0c\u539f\u6570\u636e\u5757\u4e0d\u53d8\u3002\u4f46 <strong>swapfile \u9700\u8981\u56fa\u5b9a\u7684\u7269\u7406\u5757\u4f4d\u7f6e<\/strong>\uff0c\u5185\u6838\u8981\u80fd\u7a33\u5b9a\u5730\u5f80\u8fd9\u4e9b\u5757\u91cc\u8bfb\u5199\u9875\u6570\u636e\u3002CoW \u4f1a\u8ba9\u5757\u5730\u5740\u52a8\u6001\u53d8\u5316\uff0cswap \u673a\u5236\u5c31\u65e0\u6cd5\u5de5\u4f5c\uff0c\u6240\u4ee5\u5185\u6838\u76f4\u63a5\u62d2\u7edd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6b63\u786e\u505a\u6cd5\">\u6b63\u786e\u505a\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5fc5\u987b\u5728<strong>\u6587\u4ef6\u4e3a\u7a7a\u65f6<\/strong>\u7528 <code>chattr +C<\/code> \u7981\u7528 CoW\uff0c\u7136\u540e\u518d\u5199\u5165\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u6e05\u7406\u4e4b\u524d\u5931\u8d25\u7684\u6587\u4ef6\nsudo swapoff \/swapfile 2&gt;\/dev\/null\nsudo rm -f \/swapfile\n\n# 2. \u521b\u5efa\u7a7a\u6587\u4ef6\uff08\u6ce8\u610f\u5fc5\u987b\u662f touch\uff0c\u4e0d\u80fd\u7528 dd\/fallocate\uff09\nsudo touch \/swapfile\n\n# 3. \u5173\u952e\u6b65\u9aa4\uff1a\u7981\u7528 CoW\uff08\u5fc5\u987b\u5728\u6587\u4ef6\u4e3a\u7a7a\u65f6\u6267\u884c\uff09\nsudo chattr +C \/swapfile\n\n# 4. \u73b0\u5728\u624d\u80fd\u5199\u5165\u6570\u636e\nsudo dd if=\/dev\/zero of=\/swapfile bs=1M count=1024 status=progress\n\n# 5. \u540e\u7eed\u5e38\u89c4\u6b65\u9aa4\nsudo chmod 600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile\n\n# 6. \u9a8c\u8bc1\nfree -h\nswapon --show\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u6210\u529f\u540e\u7684\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>               total        used        free      shared  buff\/cache   available\nMem:           457Mi       116Mi        22Mi       6.4Mi       336Mi       340Mi\nSwap:          1.0Gi          0B       1.0Gi\n\nNAME      TYPE  SIZE USED PRIO\n\/swapfile file 1024M   0B   -2\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>\u5c0f\u8d34\u58eb<\/strong>\uff1a\u5982\u4f55\u5224\u65ad\u81ea\u5df1\u7684\u6839\u5206\u533a\u662f\u4e0d\u662f btrfs\uff1f\u6267\u884c <code>df -T \/<\/code> \u770b Type \u5217\u5373\u53ef\u3002\u5e38\u89c1\u7684 <code>ext4<\/code> \u548c <code>xfs<\/code> \u4e0d\u9700\u8981\u8fd9\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u56db\u3001swappiness-\u8c03\u4f18\">\u56db\u3001swappiness \u8c03\u4f18<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4ec0\u4e48\u662f-swappiness\">\u4ec0\u4e48\u662f swappiness<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>vm.swappiness<\/code> \u63a7\u5236\u5185\u6838\u5c06\u5185\u5b58\u9875\u6362\u5230 swap \u7684\u503e\u5411\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u503c\u8d8a\u5927<\/strong>\uff1a\u8d8a\u503e\u5411\u4e8e\u628a\u5185\u5b58\u9875\u6362\u5230 swap<\/li>\n\n\n\n<li><strong>\u503c\u8d8a\u5c0f<\/strong>\uff1a\u8d8a\u503e\u5411\u4e8e\u4fdd\u7559\u5728 RAM\uff0c\u4f18\u5148\u56de\u6536\u6587\u4ef6 cache<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Linux \u9ed8\u8ba4\u503c\u662f <strong>60<\/strong>\uff0c\u9002\u5408\u901a\u7528\u670d\u52a1\u5668\uff0c\u4f46<strong>\u4e0d\u9002\u5408\u4f4e\u5185\u5b58 VPS<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u63a8\u8350\u503c\">\u63a8\u8350\u503c<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350\u503c<\/th><th>\u539f\u56e0<\/th><\/tr><\/thead><tbody><tr><td>\u684c\u9762 \/ \u901a\u7528\u670d\u52a1\u5668<\/td><td>60<\/td><td>\u9ed8\u8ba4\u503c\uff0c\u901a\u7528\u5e73\u8861<\/td><\/tr><tr><td><strong>\u4f4e\u5185\u5b58 VPS\uff08\u22641G\uff09<\/strong><\/td><td>10 ~ 20<\/td><td>swap \u5728\u78c1\u76d8\uff0c\u9891\u7e41\u6362\u9875\u62d6\u6162\u54cd\u5e94<\/td><\/tr><tr><td>\u4ee3\u7406 \/ \u7aef\u53e3\u8f6c\u53d1<\/td><td>10<\/td><td>\u5bf9\u5ef6\u8fdf\u654f\u611f\uff0cswap \u53ea\u505a\u4fdd\u9669<\/td><\/tr><tr><td>\u6570\u636e\u5e93\u670d\u52a1\u5668<\/td><td>1 ~ 10<\/td><td>\u907f\u514d\u70ed\u6570\u636e\u88ab\u6362\u51fa<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5173\u4e8e-swappiness-\u548c-oom-\u7684\u5e38\u89c1\u8bef\u89e3\">\u5173\u4e8e swappiness \u548c OOM \u7684\u5e38\u89c1\u8bef\u89e3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bef\u89e3<\/strong>\uff1aswappiness \u8d8a\u9ad8\u8d8a\u4e0d\u5bb9\u6613 OOM\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u771f\u76f8<\/strong>\uff1aswappiness \u4e0d\u51b3\u5b9a\"\u662f\u5426 OOM\"\uff0c\u53ea\u51b3\u5b9a\"\u65e5\u5e38\u56de\u6536\u503e\u5411\"\u3002\u5f53 RAM \u771f\u7684\u5feb\u6ee1\u65f6\uff0c<strong>\u4e0d\u7ba1 swappiness \u662f 10 \u8fd8\u662f 60\uff0c\u5185\u6838\u90fd\u4f1a\u5f3a\u5236\u4f7f\u7528 swap<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e5f\u5c31\u662f\u8bf4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>swappiness=10\uff1aRAM \u7528\u5230 ~85% \u624d\u5f00\u59cb\u6362<\/li>\n\n\n\n<li>swappiness=60\uff1aRAM \u7528\u5230 ~60% \u5c31\u5f00\u59cb\u4e3b\u52a8\u6362\u9875<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u771f\u6b63\u9632 OOM \u7684\u662f swap \u603b\u5927\u5c0f\uff0c\u800c\u4e0d\u662f swappiness\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u914d\u7f6e\u65b9\u6cd5\">\u914d\u7f6e\u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4e34\u65f6\u751f\u6548\nsudo sysctl vm.swappiness=10\n\n# \u6c38\u4e45\u751f\u6548\necho \"vm.swappiness=10\" | sudo tee -a \/etc\/sysctl.conf\nsudo sysctl -p\n\n# \u9a8c\u8bc1\ncat \/proc\/sys\/vm\/swappiness\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4e94\u3001\u4ee3\u7406--\u7aef\u53e3\u8f6c\u53d1\u573a\u666f\u7684\u5b8c\u6574\u5185\u6838\u4f18\u5316\">\u4e94\u3001\u4ee3\u7406 \/ \u7aef\u53e3\u8f6c\u53d1\u573a\u666f\u7684\u5b8c\u6574\u5185\u6838\u4f18\u5316<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u4ee3\u7406\u3001\u8f6c\u53d1\u8fd9\u7c7b\u5bf9\u5ef6\u8fdf\u548c\u5e76\u53d1\u654f\u611f\u7684\u573a\u666f\uff0c\u9664\u4e86 swappiness\uff0c\u8fd8\u6709\u51e0\u4e2a\u53c2\u6570\u503c\u5f97\u4e00\u8d77\u8c03\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo tee -a \/etc\/sysctl.conf &lt;&lt;EOF\n\n# === \u5185\u5b58\u7ba1\u7406 ===\nvm.swappiness=10\nvm.vfs_cache_pressure=50\n\n# === \u7f51\u7edc\u8f6c\u53d1\uff08\u5fc5\u5f00\uff09 ===\nnet.ipv4.ip_forward=1\nnet.ipv6.conf.all.forwarding=1\n\n# === TCP \u4f18\u5316 ===\nnet.ipv4.tcp_fastopen=3\nnet.core.default_qdisc=fq\nnet.ipv4.tcp_congestion_control=bbr\n\n# === \u8fde\u63a5\u6570\u4e0a\u9650 ===\nnet.core.somaxconn=4096\nnet.ipv4.tcp_max_syn_backlog=8192\nEOF\n\nsudo sysctl -p\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u53c2\u6570\u8bf4\u660e\">\u53c2\u6570\u8bf4\u660e<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>vfs_cache_pressure=50<\/code><\/td><td>\u964d\u4f4e\u5185\u6838\u56de\u6536\u6587\u4ef6 cache \u7684\u503e\u5411\uff0c\u51cf\u5c11\u91cd\u590d\u8bfb\u76d8<\/td><\/tr><tr><td><code>ip_forward<\/code><\/td><td>\u5185\u6838\u7ea7\u542f\u7528 IPv4 \u8f6c\u53d1\uff0c\u505a\u7aef\u53e3\u8f6c\u53d1\/NAT \u5fc5\u5f00<\/td><\/tr><tr><td><code>ipv6.conf.all.forwarding<\/code><\/td><td>IPv6 \u7248\u672c\u7684\u8f6c\u53d1\u5f00\u5173<\/td><\/tr><tr><td><code>tcp_fastopen=3<\/code><\/td><td>\u542f\u7528 TFO\uff0c\u51cf\u5c11\u63e1\u624b\u5ef6\u8fdf\uff08\u5ba2\u6237\u7aef+\u670d\u52a1\u7aef\uff09<\/td><\/tr><tr><td><code>default_qdisc=fq<\/code> + <code>tcp_congestion_control=bbr<\/code><\/td><td>\u542f\u7528 Google BBR \u62e5\u585e\u63a7\u5236\uff0c\u4ee3\u7406\u573a\u666f\u541e\u5410\u63d0\u5347\u660e\u663e<\/td><\/tr><tr><td><code>somaxconn<\/code><\/td><td>listen \u961f\u5217\u4e0a\u9650\uff0c\u4ee3\u7406\u5e76\u53d1\u8fde\u63a5\u591a\u65f6\u9632\u6b62\u4e22\u8fde\u63a5<\/td><\/tr><tr><td><code>tcp_max_syn_backlog<\/code><\/td><td>SYN \u534a\u8fde\u63a5\u961f\u5217\uff0c\u6297\u7a81\u53d1\u8fde\u63a5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">BBR \u9700\u8981\u5185\u6838 \u2265 4.9\uff0c\u73b0\u4ee3\u4e3b\u6d41\u53d1\u884c\u7248\uff08Ubuntu 20.04+\u3001Debian 10+\uff09\u90fd\u5df2\u652f\u6301\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u516d\u3001\u5b8c\u6574\u811a\u672c\">\u516d\u3001\u5b8c\u6574\u811a\u672c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u628a\u524d\u9762\u7684\u6b65\u9aa4\u6574\u5408\u6210\u4e00\u4e2a\u4e00\u952e\u811a\u672c\uff0c\u9002\u7528\u4e8e btrfs\/ext4\/xfs\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# 1GB Swap \u914d\u7f6e\u811a\u672c\uff08\u517c\u5bb9 btrfs\uff09\n\nSWAPFILE=\"\/swapfile\"\nSIZE_MB=1024\n\n# \u5982\u5df2\u5b58\u5728\u5219\u6e05\u7406\n&#91; -f \"$SWAPFILE\" ] &amp;&amp; swapoff \"$SWAPFILE\" 2&gt;\/dev\/null &amp;&amp; rm -f \"$SWAPFILE\"\n\n# \u521b\u5efa\u7a7a\u6587\u4ef6\ntouch \"$SWAPFILE\"\n\n# \u82e5\u662f btrfs \u5219\u7981\u7528 CoW\nFS_TYPE=$(df -T \/ | awk 'NR==2 {print $2}')\nif &#91; \"$FS_TYPE\" = \"btrfs\" ]; then\n    echo \"\u68c0\u6d4b\u5230 btrfs\uff0c\u7981\u7528 CoW...\"\n    chattr +C \"$SWAPFILE\"\nfi\n\n# \u5199\u5165\u6570\u636e\ndd if=\/dev\/zero of=\"$SWAPFILE\" bs=1M count=$SIZE_MB status=progress\nchmod 600 \"$SWAPFILE\"\nmkswap \"$SWAPFILE\"\nswapon \"$SWAPFILE\"\n\n# \u6dfb\u52a0\u5f00\u673a\u81ea\u52a8\u6302\u8f7d\nif ! grep -q \"$SWAPFILE\" \/etc\/fstab; then\n    echo \"$SWAPFILE none swap sw 0 0\" &gt;&gt; \/etc\/fstab\nfi\n\n# swappiness \u4f18\u5316\nsysctl vm.swappiness=10\ngrep -q \"vm.swappiness\" \/etc\/sysctl.conf || echo \"vm.swappiness=10\" &gt;&gt; \/etc\/sysctl.conf\n\necho \"=== \u914d\u7f6e\u5b8c\u6210 ===\"\nfree -h\nswapon --show\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u603b\u7ed3\">\u603b\u7ed3<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f4e\u5185\u5b58 VPS \u5fc5\u914d Swap<\/strong>\uff0c1GB \u5185\u5b58\u63a8\u8350 1~2GB Swap\uff0c0.5GB \u5185\u5b58\u63a8\u8350 1GB\u3002<\/li>\n\n\n\n<li>**btrfs \u4e0b\u5fc5\u987b\u5148 <code>touch<\/code> \u518d <code>chattr +C<\/code>**\uff0c\u6700\u540e\u624d\u5199\u6570\u636e\uff0c\u5426\u5219 <code>swapon<\/code> \u5fc5\u62a5 <code>Invalid argument<\/code>\u3002<\/li>\n\n\n\n<li><strong>swappiness \u4e0d\u9632 OOM<\/strong>\uff0c\u53ea\u5f71\u54cd\u65e5\u5e38\u6362\u9875\u503e\u5411\u3002\u4ee3\u7406\u673a\u5efa\u8bae\u8bbe\u4e3a 10\u3002<\/li>\n\n\n\n<li><strong>\u4ee3\u7406\u573a\u666f\u987a\u4fbf\u5f00\u542f BBR<\/strong>\uff0c\u541e\u5410\u63d0\u5347\u7acb\u7aff\u89c1\u5f71\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u8e29\u5751\u4e0d\u53ef\u6015\uff0c\u53ef\u6015\u7684\u662f\u8e29\u4e86\u5751\u6ca1\u8bb0\u5f55\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u5230\u540c\u6837\u9047\u5230 btrfs \u95ee\u9898\u7684\u670b\u53cb\u3002<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8bb0\u5f55\u5728\u4e00\u53f0 1C 0.5G \u914d\u7f6e\u7684 KVM VPS \u4e0a\u914d\u7f6e Swap \u7684\u5b8c\u6574\u8fc7\u7a0b\uff0c\u5305\u542b btrfs \u6587\u4ef6\u7cfb\u7edf\u4e0b\u7684\u8e29\u5751\u89e3\u51b3\u65b9\u6848\uff0c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.uika.cn\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uika.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uika.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uika.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uika.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":4,"href":"https:\/\/blog.uika.cn\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/blog.uika.cn\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/blog.uika.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uika.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uika.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}