.NET Memory Management

์ข‹์•„์š” 3

์ž˜ ์ •๋ฆฌ๋œ ๋‚ด์šฉ์ด๋„ค์š”. ์ œ๊ฐ€ ๋ดค๋˜ gc๊ด€๋ จ ์ธ๊ฐ•์ด ์žˆ๋Š”๋ฐ ์ด ์•„ํ‹ฐํด๊ณผ ๋‚ด์šฉ์ด ๋น„์Šทํ•œ๋ฐ ์ข€ ๋” ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ณ  ์žˆ์–ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

https://www.udemy.com/share/10281iBEMZdVtVRHg=/

์ด ๊ฐ•์˜๋ฅผ ์™„๋…ํ•˜๊ณ  gc์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋งŽ์ด ๋†’์•„์กŒ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ TestServer๋ผ๋Š” ํด๋ž˜์Šค๋Š” ์ฒ˜์Œ๋ด์„œ ์ฐพ์•„๋ณด๋‹ˆ msdn์— ์žˆ๋Š” ๋‹ท๋„ท api๋„ค์š”โ€ฆ ๋‚˜์ค‘์— ์ด๊ฒƒ๋„ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์œผ๋กœ ใ…Žใ…Ž

๋˜ํ•œ httpclient์˜ ๊ฒฝ์šฐ dispose๋ฅผ ํ•ด๋„ ์†Œ์ผ“ ๋ฐ”์ธ๋”ฉ์ด ํ’€๋ฆฌ์ง€ ์•Š์•„ dispose๊ฐ€ ์˜๋ฏธ์—†๋‹ค๋Š” ๊ธ€์„ ๋ดค๋Š”๋ฐ, ๊ทธ์— ๋”ฐ๋ผ ๊ผญ httpclientfactory๋ฅผ ํ†ตํ•ด httpclienthandler๋ฅผ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด๋‘๊ณ  ์ฐ์–ด๋‚ด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ˜น์‹œ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„๋“ค์„ ์œ„ํ•˜์—ฌ ๋งํฌ ๋‚จ๊น๋‹ˆ๋‹คโ€ฆ

์ข‹์•„์š” 2

์ˆ˜์ •ํ•  ๋‚ด์šฉ์ด ์ƒ๊ธด ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

๋งˆ์†Œ์—์„œ ํ•ด์ฃผ๋‹ˆ๊นŒ ๋ฏฟ๊ณ  ๊ทธ๋Ÿฌ๋ ค๋‹ˆ ํ–ˆ๋Š”๋ฐ, ์Œโ€ฆ์˜์•Œ๋ชป์ธ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”์— ๋”ฐ๋ฅด๋ฉด IHttpClientFactory๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์™„์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌ๊ฐ€๋˜์ง€๋Š” ์•Š๋‚˜๋ด…๋‹ˆ๋‹ค.

SimpleInjector๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ Generic host๋ฅผ ์“ธ ๋•Œ์ฒ˜๋Ÿผ IHttpClientFactory๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ ค ํ–ˆ๋”๋‹ˆ ๊ด€๋ จ๋œ ๊ธ€์„ ์ฐพ์•˜๋Š”๋ฐ์š”, 2019๋…„ ๊ธ€์ด๋ผ ํ™•์ธ์€ ํ•„์š”ํ•œ ๋ถ€๋ถ„ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์ € ๊ธ€์ด ์จ์งˆ ๋•Œ๋Š” .NET Core 3.1์ด ๋ง‰ ๋ฐœํ‘œ๋œ ์งํ›„์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” .NET 5์ž…๋‹ˆ๋‹ค.)

IHttpClientFactory.CreateClient() ๋ฅผ ํ†ตํ•ด HttpClient๋Š” ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฐ‘์— ์žˆ๋Š” HttpClientHandler๊ฐ€ ๊ด€๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๊ธ€๋„ ์žˆ๋„ค์š”. ์ œ๊ฐ€ ์•Œ๊ธฐ๋กœ ๋”ฐ๋กœ HttpClientHandler๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” HttpClientHandler๊ฐ€ ์žˆ๋Š” ๊ฑฐ๋กœ ์•„๋Š”๋ฐโ€ฆ

๋˜ํ•œ ์ €์ž๊ฐ€ IHttpClientFactory.CreateClient()๋กœ ์ƒ์„ฑ๋œ HttpClient์˜ ๊ฒฝ์šฐ Dispose() ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ์ด ๋ถˆํ•„์š”ํ•˜๋‹ค๋Š” ๋ง์— ๋Œ€ํ•ด์„œ ๋ฐ˜๋ฐ•ํ•˜๋ฉฐ, IDisposable ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ตฌํ˜„๋œ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ๋Š” ์‚ฌ์šฉ ํ›„ Dispose๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ƒ์‹์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์„ค์‚ฌ ์˜๋ฏธ๊ฐ€ ์—†๋”๋ผ๋„ ๊ผญ ํ˜ธ์ถœ์„ ํ•  ๊ฒƒ์„ ๊ถŒ๊ณ ํ•˜๋„ค์š”.

๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๋„ค์š”.

์ข‹์•„์š” 3