Re: Garbage Collection and Library Deployment



On Fri, 20 Feb 2009 12:02:18 -0800, Trecius <Trecius@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

[...] Because RAM is a valuable
resource for my low-end system, I call GC.Collect() and
GC.WaitForPendingFinalizers() AFTER CALLING MY METHOD, which the array is a
LOCAL variable to the method. However, looking at Task Manager, the RAM is
not released, and my application is still consuming approximately 350 MB of
RAM. Am I missing something?

Here's a sample of my code...

void MethodToCreateLargeArray()
{
object[] rgLargeArray;
// CREATE AND POPULATE rgLargeArray
}

void SomeMethod()
{
this.MethodToCreateLargeArray();
GC.Collect();
GC.WaitForPendingFinalizers();
}

That's it.

Well, you left out all the interesting parts, replacing them with a comment.

But, most likely you're just seeing normal .NET behavior. Just because you're done with the memory, that doesn't mean .NET releases the OS allocation it made in order to satisfy your need. That would be inefficient. Instead, it will continue to keep that allocation, in case you wind up allocating another huge structure again.

In general, you should not call GC.Collect() or GC.WaitForPendingFinalizers(). They are unnecessary, and usually will have counter-intuitively detrimental effects on your program's performance. For managing GC behavior, it's much more likely that you'd use the Thread.BeginCriticalRegion()/EndCriticalRegion() methods, and even that's a very rare need.

Just because RAM is "a valuable resource for your low-end system", that doesn't mean your program should be explicitly trying to manage it. Let ..NET and Windows do their job, and focus on the stuff your program actually has to do.

My second question has to do with building libraries. I have built a
library that I would like to distribute. When building the library, I ensure
the IDE is set to RELEASE. However, I've noticed that when I am using my own
library that I built in a separate project when my new application makes a
call to the library and the library has an exception, I can see all of my
code that I wrote for that library. How can I build my library that would
not allow people to see my code when the library throws an exception in debug
mode?

That depends on what you mean by "see all of my code". From your description, it sounds like you are looking at it in the debugger. If so, then all you need to do to prevent others from seeing the code is to not deliver a .pdb file and the source code along with your library.

Someone will still be able to use a tool like Red Gate's Reflector to decompile the code, but that won't be a literal representation of your actual code (e.g. no comments, variable names might be different, implementation details that are provided by the compiler may appear, etc.)

Pete
.



Relevant Pages

  • Re: Garbage Collection and Library Deployment
    ... "Peter Duniho" wrote: ... Instead, it will continue to keep that allocation, in case ... Just because RAM is "a valuable resource for your low-end system", ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: [PATCH 01/12] Blackfin arch: add peripheral resource allocation support
    ... Subject: Blackfin arch: add peripheral resource ... The dynamic resource allocation for pinmux and gpio seems to us the best ... the business of configuring pin usage. ...
    (Linux-Kernel)
  • Re: Resource allocation for multiple users
    ... > my father and I are more skilled then my brother. ... > When I assinged 3 resourcesto the insulation ... >> replace a resource by an other one. ... >>> can help microsoft project to figure out resource allocation. ...
    (microsoft.public.project)
  • Re: Resource allocation for multiple users
    ... My brother who could also complete this task works ... When I assinged 3 resourcesto the insulation ... Is there any automated way to make these resource alloations? ... >> can help microsoft project to figure out resource allocation. ...
    (microsoft.public.project)
  • Re: how do i do task,same resource, part with units X, part units
    ... Thanks for your help, the Percent allocation is what i was looking for, it ... assign a resource and in the Task Usage View, ... calculate a feasible plan for you - which is what leveling does. ...
    (microsoft.public.project)