Impact of the Garbage Collector

Asked

Viewed 305 times

8

The project here of the company is done in Windows Forms and we do not manage memory very well, because it is a legacy system that has several years running.

Currently we are facing problems with lack of memory in the computers where runs the system because of memory consumption when the system is running too high.

I have doubts about the use of GC, because I started putting in almost everywhere, but I stopped to think and I don’t know if it’s the best way, even more projects in Windows Forms.

My doubts are:

  1. When it’s good to clear your memory?
  2. The ideal time is when to open and close the screen?
  3. On all screen buttons?
  4. What problems can be caused by excessive use of GC?

How could I solve these memory loss mistakes?

  • 1

    "I started putting in almost everywhere" what that means?

  • I got excited and put in buttons and builders, for example... Where I thought I was critical, I put... But I don’t know if it’s right.

  • Put what?.

  • The GC.Collect();

1 answer

8


When it’s good to clear your memory?

Never. I’ve answered that before.

The ideal time is when to open and close the screen?

Never!

On all screen buttons?

Never!

What problems can be caused by excessive use of GC?

Breaks and loss of performance.

What harm can it do to free the memory?

That is the real question that must be asked. The problem is more about manual memory release than about leaving without doing it. Pauses will be more frequent and longer. If there was a memory problem, now there is a problem managing it. I won’t go into detail because that’s already explained in the other question.

You may have an exaggeration of memory usage due to wrong architecture or release failure. The release is automatic, but the programmer has to know how to do. He can’t keep references to the objects.

Real problem

I’ve seen cases of one window calling another calling the first, then it turns into a terrible vicious cycle. But most cases aren’t so terrible, but objects are kept alive longer than they need to.

There is a case they leave leaking longer than it should. There are pre objects that need explicit release, at least using the using.

I see most programmers making code without thinking about how it works. They just want to see the result. It doesn’t work. It takes planning. Not knowing how everything works is at the mercy of coincidence. It may or may not work.

I do not know what is the case with this application, but it is possible that you have serious problems managing the objects. Maybe you will have to make a Profiling. has tools for this:

You can’t rule out the possibility that the application simply needs more memory. There are cases like this. But before investing in hardware need to make sure that solved all software.

Now you have better subsidies to start thinking about the right problem and who knows how to ask specific questions.

  • These tools would make sure to release the memory safely then?

  • No. Nothing will magically solve the problem. They help you figure out where the error is in the application so you don’t start in the dark. Usually in these cases, you have to rewrite a lot, rearrange the application. There are cases that are so complicated that it is better to let it go and start doing it again, this time understanding everything before, otherwise the problems will happen again. Of course not all cases are so terrible.

  • Wow, I get it. It is because I have two screens that when there is excessive use of them, errors of lack of memory begin to pop up to close the system... To solve the error we have to leave the system and open again to clean everything... That’s why I thought using GC on the builders would help keep the memory at an acceptable level so as not to make a mistake... But the best is not to manually mess with the GC and use these tools to find where the bottleneck is right?

  • It only worsens the situation. If you read what I answered earlier you will see that this GC.Collect() is for experiment only, testing, not for production use. You have to see which objects are alive longer than necessary. It is not always easy to find this, because most programs are done without thinking the whole. The first step is to become aware about the lifetime of objects. You don’t just go creating and wait for them to disappear, you have to make sure the conditions for that are set. If conditions do not happen, the object is not released. It lives having memory leakage, few cases become apparent.

  • Is there any way to control the life cycle of the object? A link where I can base myself?

  • 1

    It is technical. It is to understand the whole functioning of memory, every aspect of the application. It does not have a link, there are thousands that need to be followed until you understand everything. At least in general. Of course, specific questions always fit.

  • Show! Thank you so much!

  • 1

    This may help a little: http://answall.com/q/135572/101. References often occur in graphs, which is extremely complicated when one loses control. So you have to know where each object is referenced and make sure it’s not by mistake. You don’t just go around making things work, you have to know if they’re working the right way.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.