Skip to content

Instantly share code, notes, and snippets.

@0x414c49
Created December 23, 2019 13:58
Show Gist options
  • Save 0x414c49/5bdc8e3d5fa5a9f4245e3bdc154768a2 to your computer and use it in GitHub Desktop.
Save 0x414c49/5bdc8e3d5fa5a9f4245e3bdc154768a2 to your computer and use it in GitHub Desktop.
Memory Sample1
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