473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is it smart to call gc.Collect()?

msjonathan
Hej,

We 're working on an application that displays a lot of data, we have some problems with the memory usage (displaying data in grid, see some linked data, ...) when closing the grid the memory usage does not drop, but when we call the Garbage collector in the disposed event. The memory drops phenomenal.

this is code we execute.
Expand|Select|Wrap|Line Numbers
  1. [DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  2.       private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
  3.  
  4.       /// <summary>
  5.       /// Will request to flush the memory
  6.       /// </summary>
  7.       public static void FlushMemory()
  8.       {
  9.          GC.Collect();
  10.          GC.WaitForPendingFinalizers();
  11.  
  12.          if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  13.             SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
  14.       }
  15.  
And how we call it in the disposed event

Expand|Select|Wrap|Line Numbers
  1. MemoryManagement.FlushMemory();
  2.  
My question is :) is it smart to do so? or if it just a fake solution?

Kind regards.

Greetz,

Jonathan
Dec 13 '10 #1
5 3735
GaryTexmo
1,501 Expert 1GB
There's a few threads on here about garbage collection in C#, you might be able to find them in a search. Actually, if you can't find them can you please let me know?

Anyway, the general idea is that C# is supposed to be smart enough to release memory for you when it's needed, but I've actually encountered the same issue you are in almost the same scenario, oddly enough. You would think closing the grid (an thus releasing any references) would take care of it, but for some reason it doesn't do it soon enough. You know the memory is marked to be free because the manual collect takes care of it. We ultimately had to take the same approach as you, so if you're in this position I think you're doing the right thing.

There is one gotcha though that you need to be aware of. Manually calling Collect on the GC class does a collect for all .NET processes currently running on the Framework (or at least, that's what I've understood from various readings and discussions). So when you do this, other .NET applications running on the same system could experience a performance hit, but I don't think there would be any other issues.
Dec 13 '10 #2
hej,
@GaryTexmo: thanks for the fast response. I'll perform a search on the forum. Indeed we read also about GC, and we wanted to be sure that we could perform the collect.

Kind regards.

Greetz,

Jonathan
Dec 13 '10 #3
Plater
7,872 Expert 4TB
I do not believe it effects other applications. (In fact I think it's related to your loaded AppDomain, but it might be program wide)
The description about "generations" in the gc.collect() can be confusing, but just think of it as "age" of an object reference.
I've seen a lot of people say it's bad (read: unnecessary) to call Collect().
If you are doing an mixing of managed/unmanaged(COM Objects) code, you should read into the keep-alive system if you plan to call .Collect()

Otherwise, I myself like to call it after finishing a large task. (Run task with large dataset, dispose of everything, call Collect()) I had software sitting at 600megs of used data, I called a dispose and a collect and i'd be down to 34megs.
Dec 13 '10 #4
GaryTexmo
1,501 Expert 1GB
I freely admit I can't remember where I heard about the being tied to other applications... it's been a while since I've looked into it, but I seem to recall a thread on here about it where someone either mentioned that or linked me to an article that said that.

I'd happily be wrong on that one... I was hoping msjonathan would find the thread in their search :) I haven't had a chance to go digging for it. Of course, you put the bug in my head... I can't find where I came up with that so maybe I'm just making it all up, but I had it confused anyway.

Here is something I found in one of the other threads I posted though...
http://blogs.msdn.com/b/ricom/archiv...29/271829.aspx
Dec 13 '10 #5
Plater
7,872 Expert 4TB
Yeah, I did a poor job articulating it, but Rule #2 was my situation.
Dec 14 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
16
by: LP | last post by:
Hi, Considering code below. Will it make GC to actually collect. One application creates new instances of a class from 3rd party assembly in a loop (it has to). That class doesn't have .Dispose or...
20
by: Cybertof | last post by:
Hello, Is there a good way to call a big time-consumming function from an ActiveX DLL (interoped) through a thread without blocking the main UI ? Here are the details : I have a class...
9
by: Frank Rizzo | last post by:
I understand the basic premise: when the object is out of scope or has been set to null (given that there are no funky finalizers), executing GC.Collect will clean up your resources. So I have...
3
by: jdlwright | last post by:
Hi, a customer is using our fairly memory intensive web app. in a shared hosting environment and the other day their host 'banned' (I think they just killed the process) them for memory abuse. ...
48
by: Ward Bekker | last post by:
Hi, I'm wondering if the GC.Collect method really collects all objects possible objects? Or is this still a "smart" process sometimes keeping objects alive even if they can be garbage collected?...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
3
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.