# Visual Basic > Slow Chat with the Microsoft Visual Basic team >  General Question

## dw85745

Hopefully this is not outside the forum scope.

Getting information on how the VB compiler handles various things would be helpful to know whether it is more efficient to code in a certain way.  For example a question of late is:

QUESTION
How the compiler handles string memory storage. 

I can see three possible alternatives.

1) The compiler stores all strings in a central memory location and just has a pointer reference stored on either the heap or stack.

2) The compiler uses where the string is declared to determine where the string is placed in memory.

In the simplest case if the string is declared locally (Dim strPrompt As String) and then assigned in the local procedure to the local variable, the entire string logically would be put in stack memory.

3) The compiler uses the variable to which the string is assigned to determine where it is stored (heap or stack). In this case, if no local variable is declared, and the string is assigned to a global variable, does the compiler consider the string to belong to the local procedure or does the assignment "rule" ?

If the assignment does Not rule, then the string logically would belong to the local procedure and the assignment to the global variable would cause either a copy of the string to also be stored on the heap (per your above) or pointer reference copied to the heap?

----------


## AlekseyTs[MSFT]

Are you talking about strings used by an application written in VB.NET? 

In .NET, strings are represented by System.String object, which is a reference type and is stored on the heap. Whenever compiler runs into a string literal, it emits code that allocates new System.String object and stores the reference to the object in the target variable, field, etc.  

Thanks,
Aleksey Tsingauz,
VB.NET Compiler Dev Team.

----------


## dw85745

Alex:

From my read of your post, your saying System.String object stores all string literals on the heap no matter whether they are declared Public, Private or Local.

Is this correct, or are you saying the System.String object is stored on the heap?

----------


## AlekseyTs[MSFT]

> Alex:
> 
> From my read of your post, your saying System.String object stores all string literals on the heap no matter whether they are declared Public, Private or Local.
> 
> Is this correct, or are you saying the System.String object is stored on the heap?


System.String objects and strings associated with them are stored on the heap, this is how CLR works.

Thanks,
Aleksey Tsingauz,
VB.NET Compiler Dev Team.

----------

