16

Can anyone explain me the CLR's "HighFrequencyHeap"?

codekaizen
  • 26,990
  • 7
  • 84
  • 140
Vikram
  • 485
  • 2
  • 8
  • 13

4 Answers4

12

The high frequency heap is used to store commonly used internal data structures such as the method table of types. This can be verified using WinDbg/SOS as shown below.

It is also stated in the SSCLI book (p. 235).

Here's part of the output for !eeheap

--------------------------------------
Domain 1:          006428c0
LowFrequencyHeap:  00340000(2000:2000) Size: 0x2000 (8192) bytes.
HighFrequencyHeap: 00342000(8000:2000) Size: 0x2000 (8192) bytes.
StubHeap:          Size: 0x0 (0) bytes.
Virtual Call Stub Heap:
  IndcellHeap:     Size: 0x0 (0) bytes.
  LookupHeap:      Size: 0x0 (0) bytes.
  ResolveHeap:     Size: 0x0 (0) bytes.
  DispatchHeap:    Size: 0x0 (0) bytes.
  CacheEntryHeap:  Size: 0x0 (0) bytes.
Total size:        Size: 0x4000 (16384) bytes.
--------------------------------------
Jit code heap:
LoaderCodeHeap:    004e0000(10000:1000) Size: 0x1000 (4096) bytes.
Total size:        Size: 0x1000 (4096) bytes.
--------------------------------------
Module Thunk heaps:
Module 5ef21000: Size: 0x0 (0) bytes.
Module 00342e9c: Size: 0x0 (0) bytes.
Total size:              Size: 0x0 (0) bytes.
--------------------------------------
Module Lookup Table heaps:
Module 5ef21000: Size: 0x0 (0) bytes.
Module 00342e9c: Size: 0x0 (0) bytes.
Total size:              Size: 0x0 (0) bytes.
--------------------------------------
Total LoaderHeap size:   Size: 0x13000 (77824) bytes.
=======================================
Number of GC Heaps: 1
generation 0 starts at 0x02521018
generation 1 starts at 0x0252100c
generation 2 starts at 0x02521000
ephemeral segment allocation context: none
 segment     begin allocated  size
02520000  02521000  0252e010  0xd010(53264)
Large object heap starts at 0x03521000
 segment     begin allocated  size
03520000  03521000  03523250  0x2250(8784)
Total Size:              Size: 0xf260 (62048) bytes.
------------------------------
GC Heap Size:            Size: 0xf260 (62048) bytes.

Notice the location of the high frequency heap and the garbage collected heaps. Here's the output for !dumpobject for a statically allocated instance of Program.

0:000> !dumpheap -type Program
 Address       MT     Size
0252b630 00343858       12     
total 0 objects
Statistics:
      MT    Count    TotalSize Class Name
00343858        1           12 TestBench2010.Program
Total 1 objects
0:000> !do 0252b630 
Name:        TestBench2010.Program
MethodTable: 00343858
EEClass:     0034154c
Size:        12(0xc) bytes
File:        C:\workspaces\TestBench2010\TestBench2010\bin\Debug\TestBench2010.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
00343858  4000001        4 ...Bench2010.Program  0   static 0252b630 p
0:000> !dumpheap -type Program
 Address       MT     Size
0252b630 00343858       12     
total 0 objects
Statistics:
      MT    Count    TotalSize Class Name
00343858        1           12 TestBench2010.Program
Total 1 objects
0:000> !do 0252b630 
Name:        TestBench2010.Program
MethodTable: 00343858
EEClass:     0034154c
Size:        12(0xc) bytes
File:        C:\workspaces\TestBench2010\TestBench2010\bin\Debug\TestBench2010.exe
Fields:
  MT    Field   Offset                 Type VT     Attr    Value Name
00343858  4000001        4 ...Bench2010.Program  0   static 0252b630 p

Notice the address for the static reference p in the type Program. It points to an address in the garbage collected heap. Also, notice the address of the Method Table. It points to an address in the high frequency heap.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
2

Every static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only one slot in total no matter how many instances are created. (There don't need to be any instances created for that one slot to exist though.) Note that this heap is separate from the normal garbage collected heap - it's known as a "high frequency heap", and there's one per application domain.

Stolen from here.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • you mean that garbage collector is not working with static variables? so how it can dispose from memory? – Vikram Dec 10 '10 at 04:29
  • so what is differnce between static class and static variables or method? – Vikram Dec 10 '10 at 04:33
  • 2
    The whole point of static variables is that they exist for the lifetime of the appdomain, so by definition they will never be garbage collected - at least not until the appdomain/process exits. As for the different between static classes, static methods and static variables, they're completely different and the difference is C# 101. Static variables are "class level" - one instance of the variable is created across the appdomain, scoped according to the class it's in. Static methods are methods in a class that don't need to be called on an instance - they're basically global functions. – Chris Tavares Dec 10 '10 at 05:25
  • 1
    Static classes are classes that only contain static methods, and the compiler doesn't allow you to add regular methods or a constructor or derive from it. A static class is really just a namespace mechanism to put global functions in. – Chris Tavares Dec 10 '10 at 05:26
2

There is a good overview of the various "loader heaps", of which the high frequency heap is one example, in this excellent MSDN article on .Net runtime internals.

From that article:

Frequently accessed artifacts like MethodTables, MethodDescs, FieldDescs, and Interface Maps get allocated on a HighFrequencyHeap, while less frequently accessed data structures, such as EEClass and ClassLoader and its lookup tables, get allocated on a LowFrequencyHeap. The StubHeap hosts stubs that facilitate code access security (CAS), COM wrapper calls, and P/Invoke.

codekaizen
  • 26,990
  • 7
  • 84
  • 140
  • Ugh, why can MS treat article URLs like they should be treated, as permanent resources. Will they ever learn? Fixed thanks to Internet Archive. Please consider a donation! www.archive.org – codekaizen Sep 16 '15 at 16:29
  • 1
    Or maybe [this](http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazineMay2005en-us.chm) (warning: .chm inside). – Vlad Sep 16 '15 at 18:06
  • 1
    @codekaizen: unfortunately, webarchive is currently banned in Russia. – Nick Volynkin Sep 16 '15 at 18:16
-2

The point of the high-frequency heap is so that objects that will be frequently accessed are stored close to each other. This minimizes the working set of the process.

Gabe
  • 84,912
  • 12
  • 139
  • 238