Posted SpringBoot
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鐩稿叧鍘嗗彶鏂囩珷锛?/strong>闃呰鏈枃涔嬪墠锛屾偍鍙兘闇€瑕佸厛鐪嬩笅涔嬪墠鐨勭郴鍒?/strong>馃憞锛?/strong>
銆?/span>鍐呭瓨绠$悊闅句笉闅撅紝涓浗灞变笢鎵捐摑缈?/span>銆?/span>
鎮熺氦锛?/span>甯堝倕锛屽笀鍌咃紝鍜变滑杩欏懆瀛︿範浠€涔堬紵
甯堝倕锛氬瀛愬彲鏁欎篃锛屽鏃犳澧冿紝姣忓懆涓€瀛︺€傝繖鑺傦紝鍜变滑涓昏鏄璁茶鍐呭瓨绠$悊锛屾湁鐐瑰皬闅惧憪锛屾嬁涓皬鏉垮嚦锛屽甫涓婅€虫湹濂藉ソ鍚惂銆?/span>
鎮熺氦锛氬ソ鍚э紝甯堝倕锛岃繖灏辨妸韬綋娲椾竴涓嬶紝椹笂灏辨潵銆?/span>
甯堝倕锛氣€︹€?/span>
涓€銆佸唴瀛樼鐞嗘満鍒?/span>
memcached榛樿鎯呭喌涓嬮噰鐢ㄤ簡鍚嶄负Slab Allocator锛堝唴瀛樺垎閰嶇瓥鐣ワ級鐨勬満鍒跺垎閰嶃€佺鐞嗗唴瀛樸€傚湪璇ユ満鍒跺嚭鐜颁互鍓嶏紝鍐呭瓨鐨勫垎閰嶆槸閫氳繃瀵规墍鏈夎褰曠畝鍗曞湴杩涜malloc / free鏉ヨ繘琛岀殑銆備絾鏄紝杩欑鏂瑰紡浼氬鑷村唴瀛樼鐗囷紝鍔犻噸鎿嶄綔绯荤粺鍐呭瓨绠$悊鍣ㄧ殑璐熸媴锛屾渶鍧忕殑鎯呭喌涓嬶紝浼氬鑷存搷浣滅郴缁熸瘮 memcached 杩涚▼鏈韩杩樻參銆係lab Allocator 灏辨槸涓鸿В鍐宠闂鑰岃癁鐢熺殑銆?/span>
1.1 Slab Allocator鍘熺悊
涓嬮潰鏉ョ湅鐪?Slab Allocator 鐨勫師鐞嗐€備笅闈㈡槸 memcached 鏂囨。涓殑 slab allocator 鐨勭洰鏍囷細
Theprimary goal of the slabs subsystem in memcached was to eliminate memoryfragmentation issues totally by using fixed颅size memory chunks coming from afew predetermined size classes.
涔熷氨鏄锛孲lab Allocator 鐨勫熀鏈師鐞嗘槸鎸夌収棰勫厛瑙勫畾鐨勫ぇ灏?/span>锛?/span>灏嗗垎閰嶇殑鍐呭瓨鍒嗗壊鎴愮壒瀹氶暱搴︾殑鍧?/span>锛屼互瀹屽叏瑙e喅鍐呭瓨纰庣墖闂銆?/span>
Slab Allocation 鐨勫師鐞嗙浉褰撶畝鍗曘€傚皢鍒嗛厤鐨勫唴瀛樺垎鍓叉垚鍚勭灏哄鐨勫潡锛坈hunk锛?/span>锛屽苟鎶?/span>灏哄鐩稿悓鐨勫潡鍒嗘垚缁?/span>锛坈hunk 鐨勯泦鍚堬級
鍙﹀slab allocator 杩樻湁閲嶅浣跨敤宸插垎閰嶇殑鍐呭瓨鐨勭洰鐨勩€備篃灏辨槸璇达紝鍒嗛厤鍒扮殑鍐呭瓨涓嶄細閲婃斁锛岃€屾槸閲嶅鍒╃敤銆?/span>
濂藉惂锛屼粠涓婇潰鐨勬弿杩颁腑锛屾垜浠湁蹇呰鍏堜簡瑙d竴浜涙蹇碉細
锛?锛?/span>Page锛氬垎閰嶇粰 Slab 鐨勫唴瀛樼┖闂达紝榛樿鏄?1MB銆傚垎閰嶇粰 Slab 涔嬪悗鏍规嵁 slab 鐨勫ぇ灏忓垏鍒嗘垚 chunk銆?/span>
锛?锛?/span>Chunk锛氬畠鏄皢鍐呭瓨鍒嗛厤缁欑敤鎴蜂娇鐢ㄧ殑鏈€灏忓崟鍏冿紝鐢ㄤ簬缂撳瓨璁板綍鐨勫唴瀛樼┖闂淬€?/span>
锛?锛?/span>SlabClass锛氱壒瀹氬ぇ灏忕殑 chunk 鐨勭粍銆?/span>
锛?锛?/span>item锛氱敤鎴疯瀛樺偍鐨勬暟鎹紝鍖呭惈key鍜寁alue锛屾渶缁堥兘瀛樺偍鍦╟hunk閲屻€?/span>
鐜板湪鍙互绠€鍗曠悊瑙d笅 Slab allocation锛屽湪 Memcached 涓紝鍐呭瓨棣栧厛琚垎閰嶇粰涓€涓釜pages锛屾瘡涓?page 榛樿澶у皬閮芥槸 1MB锛屽亣璁?m鍚姩鍙傛暟鏄?64 M锛堝悗闈㈤兘浠?64M 璇存槑锛夛紝閭d箞绯荤粺灏卞瓨鍦?64 涓?page銆?/span>
姣忎釜 page 琚寚瀹氫负 slab-class锛宻lab-class 鐩稿綋浜庢瘡涓?page 鐨勫睘鎬э紝鏈鎸囧畾 slab-class 鐨?page 灏辨槸鏈垎閰嶇殑鍐呭瓨銆?/span>
slab-class 鍐冲畾浜嗘瘡涓?page 瀛樺偍 item 鐨勫ぇ灏?/span>锛岀О涔嬩负 chunk锛宑hunnk 灏辨槸鐢ㄦ潵瀛樺偍 item 鐨勶紝鍦ㄥ悓涓€涓?page 涓紝鎵€鏈夌殑 chunk 澶у皬鏄竴鑷寸殑銆?/span>
涓嶅悓鐨?slab-class 鍏?chunk 澶у皬鏄笉涓€鑷寸殑锛屽熀鏈笂鏄€掑鍏崇郴锛岄粯璁ゆ槸 1.25 姣斾緥鐨勫闀挎柟寮忋€?/span>
涓轰簡鐞嗚В page锛宻lab-class锛宑hunk鐨勫叧绯伙紝鍙互杩愯鍛戒护锛歮emcached -vv,寰楀埌濡備笅鏁版嵁锛?/span>
$ memcached -vv
slab class 1: chunksize 96 perslab 10922
slab class 2: chunksize 120 perslab 8738
slab class 3: chunksize 152 perslab 6898
slab class 4: chunksize 192 perslab 5461
slab class 5: chunksize 240 perslab 4369
slab class 6: chunksize 304 perslab 3449
slab class 7: chunksize 384 perslab 2730
slab class 8: chunksize 480 perslab 2184
slab class 9: chunksize 600 perslab 1747
...
slab class 39: chunksize 524288 perslab 2
BTW锛?/span>
锛?锛塻lab class 1 鍏?chunk 澶у皬鏄?96 瀛楄妭锛岄偅涔堝搴旂殑 page 鏈€澶氬彲浠ュ瓨鍌?10922 涓?chunk銆俿labclass 2 鏍规嵁 slab class 1 * 1.25锛?6*1.25=120锛夌殑鍏紡锛宑hunk 澶у皬鏄?120 涓瓧鑺傦紝瀵瑰簲鐨?page 鏈€澶氬彲浠ュ瓨鍌?8738 涓猚hunk锛屼緷娆$被鎺ㄣ€?/span>
锛?锛夋垜浠湅鍒帮紝涓€鍏辨湁39涓猻lab锛岀涓€涓猻lab涓璫hunk澶у皬涓?6bytes锛岀浜屼釜涓?20bytes锛岀涓変釜涓?52bytes锛屾瘡涓猻lab涓璫hunk鐨勫ぇ灏忛兘涓嶄竴鏍凤紝杩欎釜chunk灏辨槸memcached鍏蜂綋瀛樺偍鏁版嵁鐨勫湴鏂广€?/span>
TBTW锛?/span>
锛?锛夊闀垮洜瀛愶細灏辨槸姣忎釜chunk涔嬮棿閫掑鐨勭郴鏁帮紝memcached鍐呭瓨鏄鍏堝垎绫荤殑锛屾瘡涓猚hunk鐨勫ぇ灏忓氨鏄€氳繃澧為暱鍥犲瓙鏉ヨ绠楃殑锛岄粯璁ょ殑澧為暱鍥犲瓙鎴栬€呰鏄闀跨郴鏁版槸1.25銆?/span>
锛?锛夐€氳繃澧為暱鍥犲瓙璁$畻绗簩涓猚hunk澶у皬锛岀涓€涓猚hunk缁勶紙涔熷氨鏄痵lab class锛夌殑澶у皬鏄?6瀛楄妭锛?B(瀛楄妭)=8b(浣?锛夛紝澧為暱鍥犲瓙鏄?.25锛岄偅涔堢浜岀粍鐨刢hunk鐨勫ぇ灏忓氨搴旇鏄?6*1.25 = 120锛岀涓夌粍鐨刢hunk澶у皬灏辨槸120*1.25 = 152銆?/span>
锛?锛夎绠椾竴涓猵age鍙互瀛樺偍鐨勪釜鏁般€傛垜浠湅绗竴涓猻lab class1 : chunk鐨勫ぇ灏忔槸96瀛楄妭锛屽叡鍙互瀛樺偍10922涓紝鎬庝箞寰楀埌鐨勫憪锛屾垜浠煡閬撲竴涓猵age鏄?M锛宲age涓槸鐢辩浉鍚屽ぇ灏忕殑chunk鏋勬垚锛岄偅涔堝厑璁稿瓨鍌ㄧ殑涓暟=1M/96B=1024*1024B/96B=10922.67鈮?0922銆?/span>
1.2 鍦?Slab 涓紦瀛樿褰曠殑鍘熺悊
涓嬮潰璇存槑 memcached 濡備綍閽堝瀹㈡埛绔彂閫佺殑鏁版嵁閫夋嫨 slab 骞剁紦瀛樺埌 chunk 涓€?/span>
memcached鏍规嵁鏀跺埌鐨勬暟鎹殑澶у皬锛岄€夋嫨鏈€閫傚悎鏁版嵁澶у皬鐨?slab锛堝涓婂浘锛夈€俶emcached 涓繚瀛樼潃slab 鍐呯┖闂?chunk 鐨勫垪琛紝鏍规嵁璇ュ垪琛ㄩ€夋嫨 chunk锛岀劧鍚庡皢鏁版嵁缂撳瓨浜庡叾涓€?/span>
閫氳繃涓婇潰鐨勫瓨鍌ㄦ柟寮忥紝鎴戜滑鑳界湅鍑篠labAllocator涔熷瓨鍦ㄤ竴浜涘紛绔€?/span>
1.3 Slab Allocator 鐨勭己鐐?/span>
SlabAllocator 瑙e喅浜嗗唴瀛樼鐗囬棶棰橈紝浣嗘柊鐨勬満鍒朵篃缁?memcached 甯︽潵浜嗘柊鐨勯棶棰樸€傝繖涓棶棰樺氨鏄紝鐢变簬鍒嗛厤鐨勬槸鐗瑰畾闀垮害鐨勫唴瀛橈紝鍥犳鏃犳硶鏈夋晥鍒╃敤鍒嗛厤鐨勫唴瀛樸€備緥濡傦紝灏?100 瀛楄妭鐨勬暟鎹紦瀛樺埌 128 瀛楄妭鐨?chunk 涓紝鍓╀綑鐨?28 瀛楄妭灏辨氮璐逛簡銆?/span>
BTW锛氳繖閲岄渶瑕佹敞鎰忕殑鏄?/span>chunk涓笉浠呬粎瀛樻斁缂撳瓨瀵硅薄鐨剉alue锛岃€屼笖淇濆瓨浜嗙紦瀛樺璞$殑key锛宔xpire time锛?flag绛夎缁嗕俊鎭€傛墍浠ュ綋set 1瀛楄妭鐨刬tem锛岄渶瑕佽繙杩?/span>澶т簬1瀛楄妭鐨勭┖闂村瓨鏀俱€?/span>
瀵逛簬璇ラ棶棰樼洰鍓嶈繕娌℃湁瀹岀編鐨勮В鍐虫柟妗堬紝浣嗗湪鏂囨。涓杞戒簡姣旇緝鏈夋晥鐨勮В鍐虫柟妗堛€傚鏋滈鍏堢煡閬撳鎴风鍙戦€佺殑鏁版嵁鐨勫叕鐢ㄥぇ灏忥紝鎴栬€呬粎缂撳瓨澶у皬鐩稿悓鐨勬暟鎹殑鎯呭喌涓嬶紝鍙浣跨敤閫傚悎鏁版嵁澶у皬鐨勭粍鐨勫垪琛紝灏卞彲浠ュ噺灏戞氮璐广€?/span>
浜屻€佹偀绾?00闂?/span>
2.1 濡備綍鏌ョ湅Memcached鍒嗛厤鐨勫唴瀛?/span>
Memcached鐨勯粯璁ゅ垎閰嶇殑鍐呭瓨鏄?4M锛屾€庝箞鏌ョ湅鍛愶細
Mac鐩存帴浣跨敤鍛戒护锛?/span>
echo stats | nc 127.0.0.1 11211
Win涓嬮渶瑕佷娇鐢╰elnet杩炴帴锛岀劧鍚庤緭鍏tats锛?/span>
telnet 127.0.0.1 11211
#杩炴帴涔嬪悗锛岃緭鍏ヤ互涓嬫寚浠わ細
stats
BTW锛氫笂闈袱绉嶆柟寮廙AC閮芥敮鎸?/span>
杩欐椂鍊欏彲浠ョ湅鍒癕emcached涓€婧滅殑淇℃伅锛?/span>
STAT pid77651
STAT uptime6208
STAT time1569563071
STAT version1.5.14
STATbytes_read 57
STATbytes_written 7579
STAT limit_maxbytes 67108864
STATaccepting_conns 1
鈥?/span>
STATdirect_reclaims 0
STATlru_bumps_dropped 0
杩欓噷鎴彇浜嗕竴閮ㄥ垎淇℃伅锛屾湁鍏冲唴瀛樺ぇ灏忕殑鍊兼槸limit_maxbytes锛屾渶澶у瓧鑺備篃灏辨槸67108864 = 64*1024*1024 = 64M銆?/span>
2.2 濡備綍璁剧疆Memcached鐨勬墍鏈夊唴瀛樺ぇ灏?/span>
Memcached鐨勯粯璁ょ殑鍐呭瓨澶у皬鏄?4M锛屾€庝箞璁剧疆锛熸瘮濡傝缃负128M锛屽彲浠ヤ娇鐢ㄥ涓嬪惎鍔ㄦ寚浠わ細
memcached -d -m 128 -p 11211
杩欐椂鍊欏湪浣跨敤鎸囦护鏌ョ湅锛?/span>
echo stats | nc 127.0.0.1 11211
杩欐椂鍊檒imit_maxbytes = 134217728=2*67108864=2*64*1024*1024=128M
2.3 memcached鐨勫唴瀛樻槸濡備綍鍒嗛厤鐨勶紵
memcached閫氳繃璁剧疆-m鏉ュ垎閰嶆渶澶у唴瀛?/span>锛岄粯璁ゆ槸64MB锛屼絾鏄?/span>涓嶆槸涓€寮€濮嬪氨浼氱洿鎺ュ悜鎿嶄綔绯荤粺鐢宠-m璁剧疆鐨勫唴瀛?/span>;鐒跺悗灏嗗唴瀛樺垎鎴愬涓猵age锛屼竴涓猵age涓?MB锛屾瘡涓猵age閲岄潰鏈夊涓浐瀹氬ぇ灏忕殑鍐呭瓨鍧梒hunk锛屽叾澶у皬鏈€灏忎负96Bytes锛屾渶澶т负1MB锛岀敱澧為暱鍥犲瓙鏉ュ喅瀹氫腑闂村唴瀛樺潡鐨勫ぇ灏忥紝姣忕澶у皬鐨刢hunk瀵瑰簲涓€涓猻lab class銆傚綋鍏朵腑鐨勪竴涓ぇ灏忕殑鍐呭瓨鍧楁墍鐢宠鐨刾age娌℃湁绌洪棿浜嗭紝鍙互鐢宠澶氫竴涓ぇ灏忕殑鍐呭瓨鍧梡age銆傛墍浠ユ渶澶х殑涓€涓猭ey+value涓嶈兘瓒呰繃1MB銆傚唴瀛樺垎閰嶇瓥鐣ュ彨鍋歴lab allocation銆?/span>
2.4涓嶅悓鐨?page锛屽叾 slab-class 鍙兘鏄竴鏍风殑
涓嶅悓鐨?page锛屽叾 slab-class 鍙兘鏄竴鏍风殑锛屾瘮濡傛墍鏈夌殑 item 閮藉皬浜?80 瀛楄妭锛屼竴鏃︾涓€涓?page 瀛樺偍婊′簡锛岄偅涔?Memcached 鍚敤绗簩涓?page 鐨勬椂鍊欙紝鍏朵粛鐒惰鎸囧畾涓?slab-class 2銆?/span>
BTW锛氭瘡涓猵age鏈€澶?M,鎵€鏈夌殑item鎬诲拰瓒呰繃1M涔嬪悗锛屽氨闇€瑕佸瓨鍌ㄥ埌slab class2
2.5 1M鐨処tem锛宻lab-class鑳藉瓨鍌ㄥ灏戜釜chunk?
濡傛灉鏈変釜 item 鏄?1M锛岄偅涔堣繖涓?slab-class 鍙兘瀛樺偍 1 涓猚hunk銆?/span>
BTW锛歴lab-class鏄浉鍚屽ぇ灏忕殑chunk鐨勯泦鍚堬紝涓€涓猵age鐨勫ぇ灏忔槸1M锛屽鏋渃hunk 澶у皬鏄?96 瀛楄妭锛岄偅涔堝搴旂殑 page 鏈€澶氬彲浠ュ瓨鍌?10922 涓猚hunk锛屽鏋渃hunk澶у皬鏄?M,閭d箞瀵瑰簲鐨刾age鏈€澶氬彲浠ュ瓨鍌?涓紝涔熷氨鏄鏋渋tem=1M,slb-class鐨勯泦鍚堜釜鏁板氨鏄?銆?/span>
涓夈€佹偀绾ゅ皬鑺?/span>
杩欎竴鑺傜殑鐭ヨ瘑鐐癸紝閮芥尯閲嶈鐨勶紝杩樻槸闇€瑕佸師鏂囧ソ濂藉搧璇汇€?/span>
鎴戝氨鏄垜锛屾槸棰滆壊涓嶄竴鏍风殑鐑熺伀銆?br class="mq-387">鎴戝氨鏄垜锛屾槸涓庝紬涓嶅悓鐨勫皬鑻规灉銆?/code>
脿鎮熺┖瀛﹂櫌锛?span class="mq-391">https://t.cn/Rg3fKJD
瀛﹂櫌涓湁Spring Boot鐩稿叧鐨勮绋嬶紒鐐瑰嚮銆?/span>闃呰鍘熸枃銆嶈繘琛屾煡鐪嬶紒
SpringBoot瑙嗛锛歨ttps://t.cn/R3QepWG
Spring Cloud瑙嗛锛歨ttps://t.cn/R3QeRZc
SpringBoot Shiro瑙嗛锛歨ttps://t.cn/R3QDMbh
SpringBoot浜ゆ祦骞冲彴锛歨ttps://t.cn/R3QDhU0
SpringData鍜孞PA瑙嗛锛歨ttps://t.cn/R1pSojf
SpringSecurity5.0瑙嗛锛歨ttps://t.cn/EwlLjHh
Sharding-JDBC鍒嗗簱鍒嗚〃瀹炴垬锛歨ttps://t.cn/E4lpD6e
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章