Garbage Collecting and real-time processing.

G

Guest

Hello, can someone please give me a tip or two on the following problem:

I have a system of linked objects which stores properties of on-screen drawn objects. When i select each object on the screen the application slowly over 10-20 seconds grows slower, and the computer runs out of memory so the disk starts spinning and the garbage collector does its thing, problem is that the applicatoin requires a bit more real-time processing and the user gets a frozen screen for 2-3 seconds at a time if not more.

Should i call the following code before someone clicks on my on-screen object so that i can release any previous allocated memory?

GC.Collect();
GC.WaitForPendingFinilizers();
GC.Collect();

Is this the only way to "Hope/pray" for a speed up?
 
B

Branimir Giurov

Hey there -

First - GC.Collect() is something you shouldn't use in a production
enviornment.

Second - if the response time is so slow, maybe you should start thinking
about a change of the design, or some of the technologies that you're
using - for example - separate the linked list operations on a different
thread, so the UI can continue to repaint, while the other thread walks
through the list. Then use a callback or something else so you can inform
the main thread about the result of the operation.

Third - You should ask yourself the following questions before starting to
search for anything else:
How big is exactly the list?
How much memory it consumes?
How big you expect the list the become?
Are you sure there are not cross references, between different nodes and
you're not going into an endless cycle?

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA


CSharpX said:
Hello, can someone please give me a tip or two on the following problem:

I have a system of linked objects which stores properties of on-screen
drawn objects. When i select each object on the screen the application
slowly over 10-20 seconds grows slower, and the computer runs out of memory
so the disk starts spinning and the garbage collector does its thing,
problem is that the applicatoin requires a bit more real-time processing and
the user gets a frozen screen for 2-3 seconds at a time if not more.
Should i call the following code before someone clicks on my on-screen
object so that i can release any previous allocated memory?
 
G

Guest

Thanks for the tip
but the problem is that i use up a lot of memory while selecting my objects on the screen, and the memory just gets taken and never released so after a couple times through the selecting of the objects on the screen memory runs out and the GC kicks in automatically and slows down everything to a crawl.

i added the 3 lines, and i solved my problem.
I no longer have memory taken and never released. If i had cross-referenced objects then the 3 lines of code woudln't free the memory no matter what, but they do, my memory usage stays constant at 20-30MB, before that, i'd consume as much memory as my system had.
 
W

Willy Denoyette [MVP]

CSharpX said:
Thanks for the tip,
i added the 3 lines, and i solved my problem.
I no longer have memory taken and never released. If i had
cross-referenced objects then the 3 lines of code woudln't free the memory
no matter what, but they do, my memory usage stays constant at 20-30MB,
before that, i'd consume as much memory as my system had.

You mean that forcing a collection releases the memory while an induced
collection doesn't release it? This is simply not possible, the GC run's
more frequently than you think, just watch the CLR memory counters using
perfmon, and use a CLR memory profiler to check your allocation patterns.
When using perfmon, the counters you should watch are the GC0, GC1 and GC2
collections for .NET CLR Memory -object, you should try to have the GC2
collections number as small as possible, you should also a CLR memory
profiler to see what objects getting relocated.

One thing to keep in mind is, that there must be something wrong with your
code when you need to call GC.Collect() .

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top