c# - How string allocates the memory in heap? -


i confused in memory allocation while creating object of string class. have created sample application demonstrates how memory being allocated when string object declared. have tried increase length of string see difference total consumed memory in heap.

my testing code here

static void main(string[] args) {     long l1 = gc.gettotalmemory(false);     long l2 = 0;      console.writeline(l1.tostring());      myfunc();      l2 = gc.gettotalmemory(false);     console.writeline(l2.tostring());     console.writeline(string.format("difference : {0}", (l2-l1)));     console.readkey(); }  private static void myfunc() {     string str = new string('a', 1); } 

the output comes when execute code:

775596 //memory @ startup 816556 //after executing function difference : 40960 

the above output same string length 0 2727. example create object of string length of 2727 out comes same above.

string str = new string('a', 2727); 

but, when increase 1 more in value , create string 2728 output comes different.

775596 //memory @ startup 822780 //after executing function difference : 47184 

i have tried in vb.net console application. in vb.net output comes same 0 797 length of string. but, gets changed when increase value 798.

i don't know how allocates memory according length of string?

the character array(string) says has 2727 items 97 byte(for character 'a'). thought multiplies value character byte. know character type has fixed length of 256 bytes. but, wondering why happens? so, have tried change character 'a' 'z'. but, result same expected.

can describe how memory gets allocated when string or other class object declared?

the problem see method of research.

int[] lengths = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 64, 128, 256, 512, 1024, 2048, 4096 }; string[] strs = new string[lengths.length]; long[] deltamemory = new long[lengths.length];  // preload functions use var str0 = new string('a', 1); var length0 = str0.length; long totalmemory0 = gc.gettotalmemory(true); long lasttotalmemory = totalmemory0;  (int = 0; < lengths.length; i++) {     strs[i] = new string((char)('a' + i), lengths[i]);     long totalmemory = gc.gettotalmemory(true);     deltamemory[i] = totalmemory - lasttotalmemory - lengths[i] * 2;     lasttotalmemory = totalmemory; }  console.writeline("intptr.size: {0}", intptr.size); (int = 0; < lengths.length; i++) {     console.writeline("for size: {0}, memory: {1}", strs[i].length, deltamemory[i]); } 

you have remember various things:

  • don't allocate memory in way other 1 measuring

  • remember first time method called must jitted. i'll operation eats memory. pre-call once methods you'll use

  • a string in .net utf-16, each character 2 two bytes (lengthts[i] * 2)

  • there surely rounding around because memory allocated in fixed chunks, of size connected size of intptr (so depending if working @ 32 or 64 bits)

the result:

intptr.size: 8 size: 1, memory: 30 size: 2, memory: 28 size: 3, memory: 26 size: 4, memory: 32 size: 5, memory: 30 size: 6, memory: 28 size: 7, memory: 26 size: 8, memory: 32 size: 9, memory: 30 size: 10, memory: 28 size: 11, memory: 26 size: 12, memory: 32 size: 13, memory: 30 size: 14, memory: 28 size: 15, memory: 26 size: 16, memory: 32 size: 17, memory: 30 size: 18, memory: 28 size: 19, memory: 26 size: 20, memory: 32 size: 21, memory: 30 size: 22, memory: 28 size: 23, memory: 26 size: 24, memory: 32 size: 25, memory: 30 size: 26, memory: 28 size: 27, memory: 26 size: 28, memory: 32 size: 29, memory: 30 size: 30, memory: 28 size: 31, memory: 26 size: 32, memory: 32 size: 64, memory: 32 size: 128, memory: 32 size: 256, memory: 32 size: 512, memory: 32 size: 1024, memory: 32 size: 2048, memory: 32 size: 4096, memory: 32 

so each string has (at 64 bits) 26-32 bytes allocated. mmmh... see skeet wrote blog post on memory allocation: http://codeblog.jonskeet.uk/2011/04/05/of-memory-and-strings/


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -