none
Why does starting processes from my program increase the ram usage of my program? RRS feed

  • Question

  • Hi I have a question. So I am working on a program that will allow you to create buttons that open other programs on your computer.(Basically a quick access thing) So the thing is that I noticed that every time I click a button to run a program(currently just open explorer) the RAM usage of my program goes up, and closing the program does not make it go back down. So I was just wondering if I am doing it right. Am I perhaps somehow creating an object that I need to disposes of or something? This is how I run the process, when you click the button it calls this event and the event runs the new process.

    public static void Execute(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("explorer", @"C:\Users\User\AppData");    
            }
    I was just wondering if I was doing it right. Is this code somehow creating a connection to the started program? I don't want my program to be able to interact or get any info from the new program I just want it to run the new program like if I clicked on the exe file or launched it from the windows start menu.

    Thursday, October 10, 2019 7:57 PM

Answers

  • The important thing is that it is very likely there is nothing more you need to do. If you do not like the way Windows handles resources then the only option is to use a different operating system.

    Microsoft has designed Windows such that it is common for people to become concerned in the manner you have. You can, as I implied previously, search for previous answers. You will learn that you need not be concerned. Otherwise there are many more important problems to solve, this one does not need to occupy your time.



    Sam Hobbs
    SimpleSamples.Info


    Saturday, October 12, 2019 9:10 PM

All replies

  • I was just wondering if I was doing it right. Is this code somehow creating a connection to the started program?

    Yes, it does. Process.Start returns a process:

    Process myProcess = System.Diagnostics.Process.Start("...");

    The object is always created by Process.Start, even if you don't store it in a variable. When you don't store it (or, for instance, you store it in a local variable and the variable goes out of scope), the object is said to "become unreachable". Contrary to other programming environments that you may have used in the past, in .Net the unreachable objects are not destroyed immediately. Instead, their memory remains allocated, and it is made available to the Garbage Collector (GC). It is not actually freed until the Garbage Collector runs and collects it, which could take a long time. By default it only occurs when there is memory pressure, otherwise it doesn't free the allocated memory.

    Thursday, October 10, 2019 8:55 PM
    Moderator
  • every time I click a button to run a program(currently just open explorer) the RAM usage of my program goes up, and closing the program does not make it go back down.

    Questions like this are asked quite often.

    How do you know that main memory usage does not go down? Are you using the Task Manager? The Task Manager is not a really good performance tool.

    If you find any of hundreds of previous replies to questions like this, you will discover that the explanation is that it is how Windows works. It worked like that for unmanged applications, not just .Net. Windows does not do clean-up (not the same thing as .Net Garbage Collection) unless it needs to. So main memory can remain allocated after it is no longer needed.

    As for the C# code you posted, you are using a static version of the Start method. So no object is created for that. Internally .Net creates unmanaged objects and .Net manages them appropriately, there is nothing more you are required to do.

    .Net uses code such as in Creating Processes - Windows applications but there is no need for you to understand any of that.



    Sam Hobbs
    SimpleSamples.Info

    Friday, October 11, 2019 7:40 PM
  • I am looking at task manager and at the ram usage chart in Visual Studio. I just happen to see it go up and not go back down. This program is just a simple program so I don't ever want to RAM usage to get too high. For example I don't want it ever using 100MBs + of RAM when there is no reason for it to. This is not a game or some video editing software. It just provides a way to create quick links to open programs, files, folders, or web pages. I am new to C# and I don't know what the best way to handle memory usage is.
    Saturday, October 12, 2019 5:26 PM
  • I just happen to see it go up and not go back down.

    That is normal.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, October 12, 2019 8:38 PM
  • I see. Would you by any chance have any tips for me on how I can best manage my programs resources?
    Saturday, October 12, 2019 8:47 PM
  • The important thing is that it is very likely there is nothing more you need to do. If you do not like the way Windows handles resources then the only option is to use a different operating system.

    Microsoft has designed Windows such that it is common for people to become concerned in the manner you have. You can, as I implied previously, search for previous answers. You will learn that you need not be concerned. Otherwise there are many more important problems to solve, this one does not need to occupy your time.



    Sam Hobbs
    SimpleSamples.Info


    Saturday, October 12, 2019 9:10 PM