Information about Garbage Collection RRS feed

  • Question

  • I have some questions about garbage collection. If I created an object how would I remove any references to it so it will be Garbage Collected? EX: String text = "Hi", or Person p = new Person(). If I were to create a game, and this game has a function that spawns enemies. I would do this by creating a class called Enemy. Then I would create a function that would use a for loop to do something like this Enemy e = new Enemy(). It would do this 4 times to create 4 enemies and it would also have to place them inside a list. If I just did list[1].remove() would that cause the enemy object at list[1] to be Garbage Collected? How often does Garbage Collection run, and what would be the best way to manage memory usage? One other question, when I create a windows form app I notice that it seems to slowly grow in memory usage and I am not even doing anything with it. All I do is create a window with 2-3 text boxes and a button and when I click the button it adds and displays the numbers I enter it to the text boxes. Why does the memory usage slowly go up as I use this app.

    Monday, September 23, 2019 3:46 PM


  • Hello,

    I would recommend reading the Microsoft documentation on garbage collection, sub-topic will be of interest in your case Induced collections.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    • Proposed as answer by EckiS Monday, September 23, 2019 6:11 PM
    • Marked as answer by LavaCreeperKing Monday, September 23, 2019 10:59 PM
    Monday, September 23, 2019 3:56 PM