Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Danger of C# Substring method?

Recently I have been reading up on some of the flaws with the Java substring method - specifically relating to memory, and how java keeps a reference to the original string. Ironically I am also developing a server application that uses C# .Net's implementation of substring many tens of times in a second. That got me thinking...

  1. Are there memory issues with the C# (.Net) string.Substring?
  2. What is the performance like on string.Substring? Is there a faster way to split a string based on start/end position?
like image 543
caesay Avatar asked Nov 08 '10 02:11

caesay


1 Answers

Just to add another perspective on this.

Out of memory (most times) does not mean you've used up all the memory. It means that your memory has been fragmented and the next time you want to allocate a chunk the system is unable to find a contiguous chunk of memory to fit your needs.

Frequent allocations/deallocations will cause memory fragmentation. The GC may not be in a position to de-fragment in time sue to the kinds of operations you do. I know the Server GC in .NET is pretty good about de-fragmenting memory but you could always starve (preventing the GC from doing a collect) the system by writing bad code.

like image 148
Jackie Kirby Avatar answered Oct 12 '22 18:10

Jackie Kirby