Created
December 23, 2019 13:58
-
-
Save 0x414c49/5bdc8e3d5fa5a9f4245e3bdc154768a2 to your computer and use it in GitHub Desktop.
Memory Sample1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
LongStringConcatWithStringBuilder(); | |
//LongStringConcat(); | |
var bytes = GC.GetTotalMemory(false); // for getting memory used by GC | |
Console.WriteLine("Bytes consumed: {0}", bytes); | |
} | |
static void LongStringConcat() | |
{ | |
var longString = DateTime.Now.ToLongDateString() + Guid.NewGuid(); | |
for (var i = 0; i < 100; i++) | |
longString += "\r\n" + DateTime.Now.ToLongDateString() + Guid.NewGuid(); | |
} | |
static void LongStringConcatWithStringBuilder() | |
{ | |
var sb = new StringBuilder(DateTime.Now.ToLongDateString() + Guid.NewGuid()); | |
for (var i = 0; i < 100; i++) | |
sb.AppendLine(DateTime.Now.ToLongDateString() + Guid.NewGuid()); | |
var longString = sb.ToString(); // this is unnecessary but you eventually need to call it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment