Re: Memory Cleanup



It seems my question is not fully understood. Calling GC.Collect is not the
issue. The issue is that my objects don't get freed.

Take the following:

class A
{
DataTable table;
}

A a = new A();
.... // Do some stuff with class a

a = null;

// Exit method, do other stuff, check memory
// The total memory has not gone down.

When running the profiler is shows that at the time of exit that class A
still had 1 instance alive at end.
Since class A has a DataTable as a member you can image how much resources
this class could take up.

We may at times want to get rid of the original class A and construct
another one so having any of the older class A instances left around isn't
very good.

-Joe

"Göran Andersson" <guffa@xxxxxxxxx> wrote in message
news:%23zX$UhX9GHA.3352@xxxxxxxxxxxxxxxxxxxxxxx
I assume that you previously programmed in an environment having a heap
that uses reference counters and deallocates objects the moment when there
are no more references to it.

The memory management in .NET does not work that way. There are no
reference counters, and nothing happens when objects go out of scope. The
objects just remain in memory waiting for the garbage collector to remove
them.

The garbage collector is only run when needed, which usually happens when
the first generation heap is full. At that time the most of the objects in
the first generation heap are unused, so the garbage collector just moves
the used objects to the second generation, and the first generation is
empty again.

If you force a garbage collection, you will push all the active objects in
the first generation into the second generation, that is why you get a
warning about so many objects being collected in the second generation.

It's normal for a program to accumulate quite some unused objects until
they are collected. That is what makes the memory management efficient.

Joe wrote:
I call GC.Collect() because memory is not freed as quickly as I need it
to be. I already set my objects to null when I'm done with them but if
that object is still referenced somewhere else, setting the original
object to null will not cause it to be freed.
My best guess is that there is still a reference to these objects
somewhere that wasn't set to null and I have no way of find it without
tracing the object through the entire application.


"MMA" <MMA@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F3257D24-EAE4-4508-B5D1-868A8C8BAB0A@xxxxxxxxxxxxxxxx
1) Not generally recomended to call GC.Collect programatically, better
to let
runtime determine whens the best time.
2) As part of your trace to see where the memory hog is, you may want to
set
your primative types to null when done, and calling dispose/and setting
to
null all object you are no longer using. Before explicitly calling
dispose.

Best of luck

"Joe" wrote:

I'm trying to track down where objects are referenced because I want to
clean up the memory when I'm done with them. I thought I found all
references but it doesn't seem that way because calling GC.Collect()
doesn't
free very much.

I ran the built-in profiler and I get a warning:
"An abnormal percentage of objects were collected in generation 2,
which is
inefficient. Investigate further by using the Objects Lifetime view."

The top 3 generation 2 are:
System.Decimal
System.Int32
System.Runtime.Serialization.ObjectHolder

I don't think there is anything I could do about Decimal and Int32 but
what
is the ObjectHolder? I do deserialize data but don't know what I can do
to
clean up this object

Thanks,
Joe





.



Relevant Pages

  • Re: FileStream.Close() & GarbageCollection - Memory Leak Question
    ... Somewhere in memory the memory allocated to the object still resides, ... If you don't clear the variable, then yes, this will throw an exception. ... concerned the GarbageCollector uses the reference and by nulling it I ... It is on the other hand the lack of live references to allocated memory that the garbage collector figures out what to collect. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Script mysteriously stops executing...
    ... memory leaks...that seems to be the case. ... Standard PHP does not have a garbage collector per se - ... These are for the cyclic reference garbage collector - this is a far ...
    (comp.lang.php)
  • Re: arrays = pointers?
    ... references, that means that updating a reference is perhaps twice as efficient in .NET as in an environment that uses for example reference counting. ... I never once brought up the question of "reference counting", nor do I find it relevant to the discussion I'm participating in. ... This is WAY faster than .NET running through all of the data structures in the memory manager, finding those that refer to a given object and modifying them. ... It's only because each and every variable "knows" what it's doing that the garbage collector can avoid having to store any additional data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: events and object lifetime
    ... onto the objects with a direct reference and had a method to set the ... Whether you use Dispose() or some other method name, ... hanging around until your application terminates and a "true" memory ... garbage collector and concentrate on programming your application. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can Dispose() release memory used by controls?
    ... Calling the Garbage Collector manually is rarely a good idea, ... Task Manager is not a good way to determine the amount of memory used ... |> private void ClearControl() ...
    (microsoft.public.dotnet.framework.windowsforms)

Loading