1

Using a memory profile on my C# desktop app I have found that strings are not being released from memory causing a slow and gradual buildup.

I have this code:

var ToYYYMMDDHHMMSS = "YYYMMDDHHMMSS";
var toYR = ToYYYMMDDHHMMSS.Substring(0, 4);
var toMN = ToYYYMMDDHHMMSS.Substring(4, 2);
var toDY =ToYYYMMDDHHMMSS.Substring(6, 2);
var toHR = ToYYYMMDDHHMMSS.Substring(8, 2);
var toMI = ToYYYMMDDHHMMSS.Substring(10, 2);
var motionPath = string.Format("{0}\\Catalogues\\{1}\\{2}\\{3}\\{4}\\{5}\\{6}", Shared.MOTION_DIRECTORY, camIndex, toYR, toMN, toDY, toHR, toMI);

Is there an alternative to using the substring? Can I use String.Format someway to get my desired result?

NB I am so sorry for my poor phrasing of my question..

var ToYYYMMDDHHMMSS = "YYYMMDDHHMMSS";

I should have added that "YYYMMDDHHMMSS" is a timestamp that always changes {apologies)

11
  • 1
    if you just need them once, define them as const not variables Commented Feb 25, 2015 at 7:18
  • Well firstly, I'd suggest that this isn't the cause of a memory leak unless you're doing something to make it a memory leak. Now, is your real value an actual DateTime value, formatted as yyyyMMddHHMMss? If so, I'd suggest parsing it as a DateTime - that'll make everything much simpler. Commented Feb 25, 2015 at 7:18
  • ToYYYMMDDHHMMSS is constant, thus all your to* can be precalculated, and lots of the path parameters are fixed. Commented Feb 25, 2015 at 7:18
  • @dotctor Hi, the values are constantly changing Commented Feb 25, 2015 at 7:19
  • 1
    Strings are subject to garbage collection just as any other object. The exception is strings that were defined in your program code, they are interned, and any strings you intern yourself, these are remembered until the program closes, but all other strings are collected when/if necessary. Commented Feb 25, 2015 at 7:25

1 Answer 1

3

My guess is that your real code has a value of something like 20150225071945 - so not actually the literal YYYYMMDDHHMMSS. If that's the case, I would parse the value as a DateTime rather than extracting substrings:

DateTime dateTime = DateTime.ParseExact(text, "yyyyMMddHHmmss",
                                        CultureInfo.InvariantCulture);
var motionPath = string.Format(@"{0}\Catalogues\{1:yyyy\\MM\\dd\\HH\\mm\\ss}",
                               Shared.MOTION_DIRECTORY, dateTime);

Note that the format string itself is a verbatim string literal, so you don't need to escape backslashes - but I've got \\ in the format string for the DateTime because the DateTime formatting code will treat \ as an escape.

An alternative would be to format each part of the date separately:

var motionPath = string.Format(@"{0}\Catalogues\{1:yyyy}\{1:MM}\{1:dd}\{1:HH}\{1:mm}\{1:ss}",
                               Shared.MOTION_DIRECTORY, dateTime);

Or use Path.Combine:

var motionPath = Path.Combine(Shared.MOTION_DIRECTORY,
                              "Catalogues",
                              dateTime.ToString("yyyy"),
                              dateTime.ToString("MM"),
                              dateTime.ToString("dd"),
                              dateTime.ToString("HH"),
                              dateTime.ToString("mm"),
                              dateTime.ToString("ss"));
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I phrased my question poorly. Still too early for me :). Your answer is what I needed - thank you very much. I will edit my question now
I suggest using Path.Combine instead of string.Format since it is related to generating a path.
@dotctor: Yes, that will end up looking uglier, but more platform-neutral. Although using forward slashes would work too. Will add an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.