Memory Management

S

Samuel

Hi

I have written an application that processes large images and therefore uses
a lot of memory.

Due to the fact that this application suppose to be widely distributed I
need to control the memory consumption, I release all objects when no long
in use and I call the garbage collector (passing 2 [for 2nd generation] as a
parameter) often.

The results that I get is not satisfactory and I wonder what can be done to
actually force the system to free all memory taken and no longer in use.

I noticed that when I run another application at the same time and the other
application took an extreme amount of memory (600mb) my application's memory
use went down to 16mb (from 40-50mb).
Can I cause that to happen from within the application (It seems that .NET
releases memory in 1 application when another application is requiring
memory). It also proved that there is no memory leak in my application

Thank you in advance ,
Samuel
 
P

Peter Duniho

Samuel said:
[...]
I noticed that when I run another application at the same time and the other
application took an extreme amount of memory (600mb) my application's memory
use went down to 16mb (from 40-50mb).

This suggests that you are in fact freeing the memory you need to free.

I also wonder how it is that in this day and age you might think that a
40-50MB working set is in any way large. :)
Can I cause that to happen from within the application (It seems that .NET
releases memory in 1 application when another application is requiring
memory). It also proved that there is no memory leak in my application

If you're sure there's no memory leak, then it sounds to me as though
you've done everything you need to (and perhaps more, in fact). If your
application does not interfere with other memory-hungry applications on
the computer, then that is all you need to do. Anything beyond that is
more likely to interfere with Windows' attempt to manage memory
efficiently, resulting in _poorer_ performance than if you simply did
nothing.

Pete
 
S

Samuel

The 40-50 figure is not the woory, it may go to as high as 150 or 200

Peter Duniho said:
Samuel said:
[...]
I noticed that when I run another application at the same time and the
other application took an extreme amount of memory (600mb) my
application's memory use went down to 16mb (from 40-50mb).

This suggests that you are in fact freeing the memory you need to free.

I also wonder how it is that in this day and age you might think that a
40-50MB working set is in any way large. :)
Can I cause that to happen from within the application (It seems that
.NET releases memory in 1 application when another application is
requiring memory). It also proved that there is no memory leak in my
application

If you're sure there's no memory leak, then it sounds to me as though
you've done everything you need to (and perhaps more, in fact). If your
application does not interfere with other memory-hungry applications on
the computer, then that is all you need to do. Anything beyond that is
more likely to interfere with Windows' attempt to manage memory
efficiently, resulting in _poorer_ performance than if you simply did
nothing.

Pete
 
H

Henning Krause [MVP - Exchange]

Hello,

calling GC.Collect will disturb internal statistics of the GC and will most
likely lead to poorer performance. If memory pressure is high, the GC will
automatically release the memory.

On option might be to use unmanaged memory for large images. AFAIK,
Paint.NET uses unmanaged memory for images.

Kind regards,
Henning Krause
 
P

Peter Duniho

Samuel said:
The 40-50 figure is not the woory, it may go to as high as 150 or 200

Even 200MB isn't an issue.

Regardless, from everything you've written so far it doesn't sound to me
as though there's an actual problem. When there's memory pressure from
other processes on the computer, the memory usage of your own
application is appropriately reduced and your own application does not
appear to interfere with other processes.

If you feel it's still a concern, you should post more specifics.
Especially you should describe how it is you are monitoring memory usage
in the first place. Jason's comments about Task Manager and monitoring
memory usage generally are accurate and you'll want to consider those
issues before wasting a lot of time dealing with this "issue".

Pete
 
S

Samuel

I will certainly look into Jason's comments but what worries me is that some
of the systems are just getting slow
 
P

Peter Duniho

Samuel said:
I will certainly look into Jason's comments but what worries me is that some
of the systems are just getting slow

"Slow" isn't necessarily your application's fault, or something you can
do anything about. You need to determine _why_ things are "slow" first.

As an example of a situation you can do nothing about: it is not
uncommon, especially on computers with relatively little installed
system memory, for an application that itself has a large memory
footprint to cause other process data to be swapped out to disk while
that application is working.

You can free the memory, but typically the other data isn't going to
come back into system memory right away. So when the user switches back
to some other application, all that data that was swapped out has to be
read back into system memory.

There's nothing you can do about that. Releasing your own data isn't
sufficient. The OS at some point simply has to read all that other data
back in, and that's going to be "slow".

Is this the problem you're dealing with? I have no idea. Your problem
description so far is simply "slow", which isn't really much to go on.
But the kinds of things you are talking about trying to do in order to
deal with memory management issues are not generally things that a
normal application would need to do, or would gain any advantage by
doing. And so far, your application sure sounds "normal".

Pete
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Samuel said:
Hi

I have written an application that processes large images and therefore uses
a lot of memory.

Due to the fact that this application suppose to be widely distributed I
need to control the memory consumption, I release all objects when no long
in use

How are you releasing them? If the objects implement IDisposable, you
should call the Dispose method when you are done with them.

Be ware of small objects that might not seem like they need disposing,
like Brush and Font. They contain windows handles, and if you don't
dispose them you might run out of windows resources.
and I call the garbage collector (passing 2 [for 2nd generation] as a
parameter) often.

There is normally no reason at all to force a garbage collection. The
garbage collector will do that when needed. Also, the garbage collector
can choose a better time to run garbage collections if you leave it be.
The results that I get is not satisfactory and I wonder what can be done to
actually force the system to free all memory taken and no longer in use.

You can't and you don't need to.
I noticed that when I run another application at the same time and the other
application took an extreme amount of memory (600mb) my application's memory
use went down to 16mb (from 40-50mb).

That's because the garbage collector kicks in when needed. There is no
reason to do a lot of work to free memory when it's not needed for
anything else.
 

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