+ 3

How To Delete Variables?

I'm writing a some kind of Terminal in C#. So, the Program has a lot of methods, about 500 just now, in the future will be few thousand. Every method callable with a specific command. If I call a command, the method ran, but after that, the variable's value still there. If you use this Program for a long time(15-30 minutes), the whole computer will be lagging. I tried the Garbage Collector, didn't helped. So, how can I delete the method's variables, after the method has been ran? Example Method: public static void get_processes() { Process[] processes = Process.GetProcesses(); Console.WriteLine("PID:\tName:"); foreach (var process in processes) { string prc = process.ToString(); Console.WriteLine(process.Id + "\t" + prc.ToString().Substring(prc.IndexOf('(') + 1, prc.Length - prc.IndexOf('(') - 2)); } }

22nd Apr 2017, 1:37 PM
Ákos Nagy
Ákos Nagy - avatar
8 Réponses
+ 2
@Ákos Nagy may I could help you through skype or like that. But without seeing anything of your code; if you develop this program in Visual Studio, then (usually) on the top right you see an updating diagram about your running program's memory usage after starting debug. If it is rising, then you are right, and memory keeps being allocated. As @Calvin Lee said, objects or values falling out of scope are being garbage collected, freed from memory automatically. If your variables keep the memory, it means, that somewhere you do store them, even without being noticed.
25th Apr 2017, 2:49 PM
Magyar Dávid
Magyar Dávid - avatar
+ 4
Thank you @Calvin Lee, this can work with strings or ints?
22nd Apr 2017, 1:42 PM
Ákos Nagy
Ákos Nagy - avatar
+ 4
I think even without dereferencing the array pointer, it should be fall out of scope at the end of the function. The garbage collector will take care of freeing the memory for you.
22nd Apr 2017, 1:53 PM
Calviղ
Calviղ - avatar
+ 2
@Calvin Lee ollydbg shows they are there, fter the method ran.
22nd Apr 2017, 1:29 PM
Ákos Nagy
Ákos Nagy - avatar
+ 2
@Calvin Lee I modified the question.
22nd Apr 2017, 1:38 PM
Ákos Nagy
Ákos Nagy - avatar
+ 1
Declare local variables in the function. It wont take up any memory after the function has been called and exit.
22nd Apr 2017, 1:27 PM
Calviղ
Calviղ - avatar
+ 1
Just show us one of the function please..
22nd Apr 2017, 1:30 PM
Calviղ
Calviղ - avatar
+ 1
processes = null; Null the array pointer before function exit.
22nd Apr 2017, 1:40 PM
Calviղ
Calviղ - avatar