Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 87151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:21:28+00:00 2026-05-10T22:21:28+00:00

A recent question about string literals in .NET caught my eye. I know that

  • 0

A recent question about string literals in .NET caught my eye. I know that string literals are interned so that different strings with the same value refer to the same object. I also know that a string can be interned at runtime:

string now = DateTime.Now.ToString().Intern();  

Obviously a string that is interned at runtime resides on the heap but I had assumed that a literal is placed in the program’s data segment (and said so in my answer to said question). However I don’t remember seeing this anywhere. I assume this is the case since it’s how I would do it and the fact that the ldstr IL instruction is used to get literals and no allocation seems to take place seems to back me up.

To cut a long story short, where do string literals reside? Is it on the heap, the data segment or some-place I haven’t thought of?


Edit: If string literals do reside on the heap, when are they allocated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:21:29+00:00Added an answer on May 10, 2026 at 10:21 pm

    Strings in .NET are reference types, so they are always on the heap (even when they are interned). You can verify this using a debugger such as WinDbg.

    If you have the class below

       class SomeType {       public void Foo() {          string s = 'hello world';          Console.WriteLine(s);          Console.WriteLine('press enter');          Console.ReadLine();       }    } 

    And you call Foo() on an instance, you can use WinDbg to inspect the heap.

    The reference will most likely be stored in a register for a small program, so the easiest is to find the reference to the specific string is by doing a !dso. This gives us the address of our string in question:

    0:000> !dso OS Thread Id: 0x1660 (0) ESP/REG  Object   Name 002bf0a4 025d4bf8 Microsoft.Win32.SafeHandles.SafeFileHandle 002bf0b4 025d4bf8 Microsoft.Win32.SafeHandles.SafeFileHandle 002bf0e8 025d4e5c System.Byte[] 002bf0ec 025d4c0c System.IO.__ConsoleStream 002bf110 025d4c3c System.IO.StreamReader 002bf114 025d4c3c System.IO.StreamReader 002bf12c 025d5180 System.IO.TextReader+SyncTextReader 002bf130 025d4c3c System.IO.StreamReader 002bf140 025d5180 System.IO.TextReader+SyncTextReader 002bf14c 025d5180 System.IO.TextReader+SyncTextReader 002bf15c 025d2d04 System.String    hello world             // THIS IS THE ONE 002bf224 025d2ccc System.Object[]    (System.String[]) 002bf3d0 025d2ccc System.Object[]    (System.String[]) 002bf3f8 025d2ccc System.Object[]    (System.String[]) 

    Now use !gcgen to find out which generation the instance is in:

    0:000> !gcgen 025d2d04  Gen 0 

    It’s in generation zero – i.e. it has just be allocated. Who’s rooting it?

    0:000> !gcroot 025d2d04  Note: Roots found on stacks may be false positives. Run '!help gcroot' for more info. Scan Thread 0 OSTHread 1660 ESP:2bf15c:Root:025d2d04(System.String) Scan Thread 2 OSTHread 16b4 DOMAIN(000E4840):HANDLE(Pinned):6513f4:Root:035d2020(System.Object[])-> 025d2d04(System.String) 

    The ESP is the stack for our Foo() method, but notice that we have a object[] as well. That’s the intern table. Let’s take a look.

    0:000> !dumparray 035d2020 Name: System.Object[] MethodTable: 006984c4 EEClass: 00698444 Size: 528(0x210) bytes Array: Rank 1, Number of elements 128, Type CLASS Element Methodtable: 00696d3c [0] 025d1360 [1] 025d137c [2] 025d139c [3] 025d13b0 [4] 025d13d0 [5] 025d1400 [6] 025d1424 ... [36] 025d2d04  // THIS IS OUR STRING ... [126] null [127] null 

    I reduced the output somewhat, but you get the idea.

    In conclusion: strings are on the heap – even when they are interned. The interned table holds a reference to the instance on the heap. I.e. interned strings are not collected during GC because the interned table roots them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I contacted Adobe Support, there is no way in Flex… May 11, 2026 at 7:20 pm
  • Editorial Team
    Editorial Team added an answer This is not a programming question, but the answer is… May 11, 2026 at 7:20 pm
  • Editorial Team
    Editorial Team added an answer Did it. Created an action that returned void, and JSON-serialized… May 11, 2026 at 7:20 pm

Related Questions

A recent question came up about using String.Format(). Part of my answer included a
As a follow-up to my recent question about .NET Compact Framework debugging, I am
Following on from my recent question on Large, Complex Objects as a Web Service
In my (PHP) web app, I have a part of my site that keeps

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.