%3CLINGO-SUB%20id%3D%22lingo-sub-826622%22%20slang%3D%22en-US%22%3EASP.net%20segment%20heap%20sizes%20%E2%80%93%20or%20how%20much%20virtual%20memory%20my%20web-app%20will%20need%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-826622%22%20slang%3D%22en-US%22%3E%3CP%3EMany%20times%2C%20customers%20come%20to%20me%20saying%20they%20have%20a%20feeling%20that%20their%20ASP.net%20application%20takes%20up%20more%20memory%20then%20it%20did%20before%2C%20especially%20if%20they%20are%20migrating%20from%20the%20.Net%202.0%20Runtime%20to%20the%20.Net%204.0%20Runtime%20and%20from%20a%2032%20bit%20architecture%20to%20a%2064%20bit%20architecture.%20Some%20time%20ago%2C%20I%20wrote%20a%20small%20cheat%20on%20.Net%20segment%20size%20vs%20Architecture%2C%20which%20you%20can%20find%20listed%20here%3A%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3CA%20style%3D%22background-color%3A%20%23ffffff%3B%22%20href%3D%22http%3A%2F%2Flinqto.me%2Fn%2FAspNetMemory%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Flinqto.me%2Fn%2FAspNetMemory%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EToday%2C%20I%20would%20like%20to%20go%20into%20a%20little%20more%20detail%20on%20how%20we%20go%20about%20computing%20the%20memory%20needed%20at%20startup%20by%20an%20ASP.net%20architecture%2C%20based%20on%20the%20machine%20we%20are%20running%20on%2C%20since%20there%20are%20several%20factors%20that%20come%20into%20play%20when%20calculating%20this%20sum.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHeaps%2C%20Heaps%20and%20more%20Heaps%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E.Net%20stores%20most%20of%20the%20variables%20you%20will%20create%20(except%20for%20the%20value%20types)%20on%20a%20data-structure%20called%20the%20heap.%20This%20lives%20in%20the%20process%20address%20space%20and%20grows%20as%20more%20and%20more%20variables%20are%20needed%20and%20allocated%20by%20the%20application.%20The%20key%20is%20the%20%E2%80%98growing%20when%20needed%E2%80%99.%20If%20the%20.Net%20Framework%20simply%20waits%20to%20execute%20an%20instruction%20calling%20%E2%80%98new%E2%80%99%20to%20allocate%20a%20variable%2C%20this%20would%20be%20very%20bad%20for%20performance%20reasons%20we%20will%20not%20discuss%20here.%20Hence%20the%20heap%20pre-allocates%20entire%20regions%20of%20memory%20(called%20segments)%20which%20can%20then%20be%20used%20to%20store%20variables.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThe%20.Net%20Managed%20Heap%20is%20actually%20two%20data%20structures%3A%20the%20Small%20Object%20Heap%20and%20the%20Large%20Object%20Heap.%20The%20Small%20Object%20Heap%20(SOH)%20is%20used%20to%20store%20smaller%20sized%20objects.%20Everything%20that%20is%20larger%20in%20size%20than%2086%20Kb%20is%20placed%20on%20the%20Large%20Object%20Heap.%20You%20can%20learn%20more%20about%20the%20two%20by%20reading%20this%20article%20on%20my%20friend%20Tess%E2%80%99s%20blog%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Ftess%2Farchive%2F2006%2F06%2F22%2F643309.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fblogs.msdn.com%2Fb%2Ftess%2Farchive%2F2006%2F06%2F22%2F643309.aspx%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESuffice%20to%20say%2C%20that%20when%20each%20of%20the%20two%20heaps%20is%20initialized%2C%20just%20before%20your%20application%20is%20loaded%20into%20the%20w3wp.exe%20process%2C%20a%20heap%20segment%20will%20be%20reserved%20for%20the%20SOH%20and%20a%20second%20heap%20segment%20will%20be%20reserved%20for%20the%20LOH.%20Hence%2C%20we%20wind%20up%20with%20two%20heap%20segments%20of%20process%20address%20space%20that%20are%20reserved%20from%20the%20get%20go.%20To%20understand%20more%20about%20process%20address%20spaces%20and%20reserved%20memory%2C%20please%20go%20through%20the%20following%20article%20on%20memory%20management%20in%20a%20Windows%20process%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CA%20href%3D%22http%3A%2F%2Fblogs.microsoft.co.il%2Fsasha%2F2016%2F01%2F05%2Fwindows-process-memory-usage-demystified%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fblogs.microsoft.co.il%2Fsasha%2F2016%2F01%2F05%2Fwindows-process-memory-usage-demystified%2F%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23800000%22%3E%3CSTRONG%3EWhat%E2%80%99s%20inside%20the%20box%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EYour%20computer%20%2F%20server%20that%20is%20running%20the%20ASP.net%20application%20you%20have%20just%20written%2C%20be%20it%20a%20virtual%20machine%20or%20a%20physical%20machine%2C%20will%20be%20equipped%20with%20a%20CPU.%20The%20central%20processing%20unit%20can%20(and%20normally%20does)%20have%20more%20than%20one%20core.%20For%20modern%20processors%2C%20they%20tend%20to%20have%20multiple%20cores%2C%20multiple%20processors%20on%20the%20same%20chip.%20Each%20of%20the%20cores%20may%20be%20hyper%20threaded%2C%20resulting%20in%20the%20fact%20that%20Windows%20may%20see%20double%20the%20number%20of%20processors%20if%20each%20core%20is%20hyperthreaded.%3C%2FP%3E%0A%3CP%3EIf%20you%20start%20the%20Windows%20Task%20Manager%2C%20you%20can%20see%20how%20many%20cores%20you%20have%20available%20by%20looking%20at%20the%20Performance%20tab%2C%20on%20the%20CPU%20resources.%20If%20you%20only%20have%20one%20graph%2C%20make%20sure%20that%20you%20have%20selected%20the%20option%20(from%20the%20context%20menu)%20to%20Show%20the%20Logical%20Processors%20(see%20screenshot).%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128914i734AAC895AEA81E2%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESo%20why%20is%20this%20important%3F%20Because%20the%20.Net%20Framework%20will%20try%20and%20take%20maximum%20advantage%20of%20the%20architecture%20of%20the%20server%20%2F%20machine%20it%20is%20running%20on%2C%20and%20will%20make%20use%20of%20each%20logical%20core%20available.%20How%20can%20it%20do%20this%3F%20One%20way%20is%20by%20creating%20multiple%20Managed%20Heaps%20instead%20of%20just%20one.%20In%20this%20way%2C%20the%20memory%20allocation%20operations%20that%20are%20needed%20can%20be%20performed%20by%20the%20processor%20the%20heap%20is%20allocated%20to.%20Hence%2C%20you%20will%20have%20as%20many%20.Net%20Heaps%20(a%20SOH%20and%20LOH)%20as%20you%20have%20processors.%3C%2FP%3E%0A%3CP%3EFor%20the%20example%20screenshot%20above%2C%20the%20machine%20has%20eight%20processor%20cores.%20If%20we%20fire%20up%20an%20ASP.net%20application%2C%20the%20.Net%20Runtime%20will%20create%208%20SOH%20and%20LOH%20heaps%2C%20each%20of%20which%20will%20reserve%20an%20initial%20segment%20of%20memory.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23800000%22%3E%3CSTRONG%3EDon%E2%80%99t%20forget%20about%20the%20architecture%20%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThe%20architecture%20that%20your%20computer%20runs%20is%20also%20a%20factor%20in%20the%20equation.%20Older%20servers%20used%20to%20run%20on%2032%20bit%20architectures%2C%20meaning%20that%20each%20pointer%20(number%20that%20points%20to%20an%20address%20in%20the%20process%20address%20space)%20had%2032%20digits%20which%20could%20be%20either%201%20or%200.%20More%20recent%20machines%20have%2064%20bit%20architectures%2C%20meaning%20the%20pointers%20are%2064%20digits%20log.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThe%2064-bit%20architecture%20pointers%20are%20twice%20the%20size%20of%20the%2032-bit%20ones%2C%20and%20hence%20we%20can%20represent%20a%20whole%20lot%20more%20virtual%20process%20address%20space%20on%20such%20an%20architecture.%20The%20.Net%20Framework%20can%20operate%20both%20on%2032%20and%2064%20bit%20architectures%2C%20but%20will%20create%20bigger%20or%20smaller%20heap%20segments%20base%20on%20the%20architecture%20it%20is%20running%20on.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23800000%22%3E%3CSTRONG%3EPutting%20it%20all%20together%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ETo%20answer%20the%20question%3A%20how%20much%20memory%20is%20reserved%20by%20the%20.Net%20Framework%20at%20the%20start%20of%20my%20ASP.net%20application%2C%20we%20need%20to%20take%20into%20consideration%20the%20factors%20listed%20above%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EEach%20managed%20heap%20is%20composed%20of%20two%20heaps%3A%20SOH%20and%20LOH%3C%2FLI%3E%0A%3CLI%3EThere%20will%20be%20as%20many%20heaps%20as%20there%20are%20logical%20processors%20on%20the%20machine%3C%2FLI%3E%0A%3CLI%3EHeap%20segment%20size%20depends%20on%20machine%20architecture.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWith%20this%20in%20mind%2C%20we%20can%20now%20look%20at%20the%20.Net%20segment%20sizes%20based%20on%20architecture%2C%20Runtime%20version%20and%20heap%20type%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3ENET%202.0%3C%2FSTRONG%3Eon%20%3CSTRONG%3Ex86%3C%2FSTRONG%3Earchitectures%3A%20%3CSTRONG%3E64%20Mb%3C%2FSTRONG%3Efor%20small%20object%20segment%20per%20processor%20and%20%3CSTRONG%3E32%20Mb%3C%2FSTRONG%3Efor%20large%20object%20segment%20per%20processor%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ENET%202.0%3C%2FSTRONG%3Eon%20%3CSTRONG%3Ex64%3C%2FSTRONG%3Earchitectures%3A%20%3CSTRONG%3E512%20Mb%3C%2FSTRONG%3Efor%20small%20object%20segment%20per%20processor%20and%20%3CSTRONG%3E128%20Mb%3C%2FSTRONG%3Efor%20large%20object%20segment%20per%20processor%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ENET%204.0%3C%2FSTRONG%3Eon%20%3CSTRONG%3Ex86%20%3C%2FSTRONG%3Earchitectures%3A%2064%20Mb%20for%20small%20object%20segment%20per%20processor%20and%20%3CSTRONG%3E32%20Mb%3C%2FSTRONG%3Efor%20large%20object%20segment%20per%20processor%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ENET%204.0%3C%2FSTRONG%3Eon%20%3CSTRONG%3Ex64%20%3C%2FSTRONG%3Earchitectures%3A%20%3CSTRONG%3E1024%20Mb%3C%2FSTRONG%3Efor%20small%20object%20segment%20per%20processor%20and%20%3CSTRONG%3E256%20Mb%3C%2FSTRONG%3Efor%20large%20object%20segment%20per%20processor%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EHere%20is%20a%20small%20side%20note%20add%20on%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIf%20you%20are%20running%20on%20a%2032%20bit%20architecture%2C%20based%20on%20the%20number%20of%20processors%2C%20the%20segment%20size%20will%20shrink%20as%20such%3A%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20-%26nbsp%3B%20If%20you%20are%20running%20on%20a%20machine%20with%20more%20than%204%20logical%20processors%2C%20the%20segment%20sizes%20for%20the%20managed%20heaps%20will%20be%3A%20%3CSTRONG%3E32%20Mb%3C%2FSTRONG%3Efor%20the%20small%20object%20heap%20and%20%3CSTRONG%3E16%20Mb%26nbsp%3B%3C%2FSTRONG%3Efor%20the%20large%20object%20heap%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20-%20If%20you%20are%20running%20on%20a%20machine%20with%20more%20than%208%20logical%20processors%2C%20the%20sizes%20are%20as%20follows%3A%20%3CSTRONG%3E16%20Mb%3C%2FSTRONG%3Efor%20the%20small%20object%20heap%20and%20%3CSTRONG%3E8%20Mb%3C%2FSTRONG%3Efor%20the%20large%20object%20heap%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThis%20is%20done%20to%20prevent%20the%20.Net%20Runtime%20from%20reserving%20more%20memory%20than%20is%20possible%20in%20a%2032-bit%20address%20space%20(2%20Gb%20max%20assuming%20that%20you%20are%20not%20using%20the%20%2F3GB%20switch)%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20just%20for%20completeness%2C%20here%20is%20a%20table%20with%20all%20segment%20sizes%20and%20possibilities%20for%20the%20initial%20segment%20size%3A%3C%2FP%3E%0A%3CTABLE%20width%3D%22940%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E%3CSTRONG%3EFramework%20Version%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E%3CSTRONG%3EArchitecture%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E%3CSTRONG%3E%23%20of%20Logical%20Processors%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E%3CSTRONG%3ESmall%20Object%20Heap%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E%3CSTRONG%3ELarge%20Object%20Heap%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E%3CSTRONG%3ETotal%20par%20processor%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%202%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX86%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3ENb%20proc%20%26lt%3B%3D%204%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E64%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E32%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E96%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%202%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX86%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E4%20%26lt%3B%20Nb%20proc%20%26lt%3B%3D%208%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E32%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E16%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E48%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%202%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX86%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E8%20%26lt%3B%20Nb%20proc%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E16%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E8%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E24%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%202%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX64%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EAny%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E512%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E128%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E640%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%204.x%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX86%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3ENb%20proc%20%26lt%3B%3D%204%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E64%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E32%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E96%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%204.x%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX86%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E8%20%26lt%3B%20Nb%20proc%20%26lt%3B%3D8%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E32%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E16%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E48%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%204.x%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX86%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E8%20%26lt%3B%20Nb%20proc%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E16%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E8%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E24%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E.Net%20Framework%204.x%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EX64%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3EAny%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E1024%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E256%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%20width%3D%22157%22%3E%3CP%3E1280%20Mb%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ewritten%20by%3A%20%3CA%20href%3D%22http%3A%2F%2Fwww.linqto.me%2Fabout%2Fpcociuba%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPaul%20Cociuba%3C%2FA%3E%3CBR%20%2F%3Ereviewed%20by%3A%20Muna%20AlHasan%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-826622%22%20slang%3D%22en-US%22%3E%3CP%3EMany%20times%2C%20customers%20come%20to%20me%20saying%20they%20have%20a%20feeling%20that%20their%20ASP.net%20application%20takes%20up%20more%20memory%20then%20it%20did%20before%2C%20especially%20if%20they%20are%20migrating%20from%20the%20.Net%202.0%20Runtime%20to%20the%20.Net%204.0%20Runtime%20and%20from%20a%2032%20bit%20architecture%20to%20a%2064%20bit%20architecture.%20This%20article%20will%20try%20and%20explain%20how%20and%20why%20ASP.net%20takes%20up%20memory%20as%20per%20your%20machine's%20architecture.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

Many times, customers come to me saying they have a feeling that their ASP.net application takes up more memory then it did before, especially if they are migrating from the .Net 2.0 Runtime to the .Net 4.0 Runtime and from a 32 bit architecture to a 64 bit architecture. Some time ago, I wrote a small cheat on .Net segment size vs Architecture, which you can find listed here:
                http://linqto.me/n/AspNetMemory


Today, I would like to go into a little more detail on how we go about computing the memory needed at startup by an ASP.net architecture, based on the machine we are running on, since there are several factors that come into play when calculating this sum.

Heaps, Heaps and more Heaps

.Net stores most of the variables you will create (except for the value types) on a data-structure called the heap. This lives in the process address space and grows as more and more variables are needed and allocated by the application. The key is the ‘growing when needed’. If the .Net Framework simply waits to execute an instruction calling ‘new’ to allocate a variable, this would be very bad for performance reasons we will not discuss here. Hence the heap pre-allocates entire regions of memory (called segments) which can then be used to store variables.

The .Net Managed Heap is actually two data structures: the Small Object Heap and the Large Object Heap. The Small Object Heap (SOH) is used to store smaller sized objects. Everything that is larger in size than 86 Kb is placed on the Large Object Heap. You can learn more about the two by reading this article on my friend Tess’s blog:

                http://blogs.msdn.com/b/tess/archive/2006/06/22/643309.aspx

Suffice to say, that when each of the two heaps is initialized, just before your application is loaded into the w3wp.exe process, a heap segment will be reserved for the SOH and a second heap segment will be reserved for the LOH. Hence, we wind up with two heap segments of process address space that are reserved from the get go. To understand more about process address spaces and reserved memory, please go through the following article on memory management in a Windows process:

                http://blogs.microsoft.co.il/sasha/2016/01/05/windows-process-memory-usage-demystified/

What’s inside the box

Your computer / server that is running the ASP.net application you have just written, be it a virtual machine or a physical machine, will be equipped with a CPU. The central processing unit can (and normally does) have more than one core. For modern processors, they tend to have multiple cores, multiple processors on the same chip. Each of the cores may be hyper threaded, resulting in the fact that Windows may see double the number of processors if each core is hyperthreaded.

If you start the Windows Task Manager, you can see how many cores you have available by looking at the Performance tab, on the CPU resources. If you only have one graph, make sure that you have selected the option (from the context menu) to Show the Logical Processors (see screenshot).

 

clipboard_image_0.png

So why is this important? Because the .Net Framework will try and take maximum advantage of the architecture of the server / machine it is running on, and will make use of each logical core available. How can it do this? One way is by creating multiple Managed Heaps instead of just one. In this way, the memory allocation operations that are needed can be performed by the processor the heap is allocated to. Hence, you will have as many .Net Heaps (a SOH and LOH) as you have processors.

For the example screenshot above, the machine has eight processor cores. If we fire up an ASP.net application, the .Net Runtime will create 8 SOH and LOH heaps, each of which will reserve an initial segment of memory.

Don’t forget about the architecture

The architecture that your computer runs is also a factor in the equation. Older servers used to run on 32 bit architectures, meaning that each pointer (number that points to an address in the process address space) had 32 digits which could be either 1 or 0. More recent machines have 64 bit architectures, meaning the pointers are 64 digits log.

The 64-bit architecture pointers are twice the size of the 32-bit ones, and hence we can represent a whole lot more virtual process address space on such an architecture. The .Net Framework can operate both on 32 and 64 bit architectures, but will create bigger or smaller heap segments base on the architecture it is running on.

Putting it all together

To answer the question: how much memory is reserved by the .Net Framework at the start of my ASP.net application, we need to take into consideration the factors listed above:

  • Each managed heap is composed of two heaps: SOH and LOH
  • There will be as many heaps as there are logical processors on the machine
  • Heap segment size depends on machine architecture.

With this in mind, we can now look at the .Net segment sizes based on architecture, Runtime version and heap type:

  • NET 2.0 on x86 architectures: 64 Mb for small object segment per processor and 32 Mb for large object segment per processor
  • NET 2.0 on x64 architectures: 512 Mb for small object segment per processor and 128 Mb for large object segment per processor
  • NET 4.0 on x86 architectures: 64 Mb for small object segment per processor and 32 Mb for large object segment per processor
  • NET 4.0 on x64 architectures: 1024 Mb for small object segment per processor and 256 Mb for large object segment per processor

Here is a small side note add on:

  • If you are running on a 32 bit architecture, based on the number of processors, the segment size will shrink as such:
            -  If you are running on a machine with more than 4 logical processors, the segment sizes for the managed heaps will be: 32 Mb for the small object heap and 16 Mb for the large object heap
            - If you are running on a machine with more than 8 logical processors, the sizes are as follows: 16 Mb for the small object heap and 8 Mb for the large object heap

This is done to prevent the .Net Runtime from reserving more memory than is possible in a 32-bit address space (2 Gb max assuming that you are not using the /3GB switch) 

 

And just for completeness, here is a table with all segment sizes and possibilities for the initial segment size:

Framework Version

Architecture

# of Logical Processors

Small Object Heap

Large Object Heap

Total par processor

.Net Framework 2

X86

Nb proc <= 4

64 Mb

32 Mb

96 Mb

.Net Framework 2

X86

4 < Nb proc <= 8

32 Mb

16 Mb

48 Mb

.Net Framework 2

X86

8 < Nb proc

16 Mb

8 Mb

24 Mb

.Net Framework 2

X64

Any

512 Mb

128 Mb

640 Mb

.Net Framework 4.x

X86

Nb proc <= 4

64 Mb

32 Mb

96 Mb

.Net Framework 4.x

X86

8 < Nb proc <=8

32 Mb

16 Mb

48 Mb

.Net Framework 4.x

X86

8 < Nb proc

16 Mb

8 Mb

24 Mb

.Net Framework 4.x

X64

Any

1024 Mb

256 Mb

1280 Mb

 

written by: Paul Cociuba
reviewed by: Muna AlHasan