Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Also got the problem!

 

Well, here's another guy having severe experiences with this "small app-huge memory allocation"-problem. I have written 3 small apps (VB.net) that are now constantly running (simultaniuosly) on my computer. They range from about 15 lines to maybe 200 lines of code each. (i.e. very small apps) Memory consumption at startup for each of these sweeties is between 12 and 14 Mb(!) with none of them are making any strange calls etc. For example that 15-liner only uses a few buttons and a few text boxes.

 

I got very surprised trying this "minimize and restore" since it actually makes these apps reduce memory allocation to about 400-500kb minimized and about 2Mb restored!. So instead of together using about 35 Mb they are now, after minimize+restore, 7-8Mb in total size. This makes quite a huge difference since I also have other software running trying to consume my precious RAM. Someone have any idea of how to make the .net code behave a bit nicer already at startup? Doesn't seem like everyone have this problem or maybe they just haven't checked their taskmanager...:)

 

Thanks in advance!

 

-Stelin-

 

---------------------------------------------------------------

I have found that VB.net 's memory handling is very erratic at best.

 

Problem:

I built a small app in VB6 that pings a server periodically for information. The app uses approx 4500kb in memory. I built a similar app in .Net and it uses 14000kb! and as the program is working it gets larger...and larger.

 

Strange I thought, I must be allowing objects to live without disposing of the properly, I then went through all my code killing everything that wasnt nailed down. Still the same.

 

After a couple of days trying to get the program to conform, I noticed that if I minimised the main window to the task bar then set its state to normal again the memory usage would promply go down to 3000kb. As the program was working the memory usage would rise again, so I would perform the minimize...maximize (normal) trick again and yes...it went back to 3000k again.

 

At this point I tried playing around with the Garbage collector, fearing that it wasnt working properly, but that had no effect.

 

So in a nutshell I have a program that starts off using 14000kb (9500kb more than its VB6 equivilent) of memory until I minimize then maximize (normal) its main form, then the memory usage goes down by 11000kb!

 

The program runs for long periods (weeks/months) and the only way to stop it using vast amounts of memory is to periodically make the app automatically minimize then maximize!!

 

Now to me this seems rather odd! has anyone else noticed this?

 

Is this a bug?

 

Does anyone know a fix??

 

Thanks

Imogen

 

PS I have built the final exe's in release mode etc....

;) ;)
Posted

I've had the same problem you to. Even with VB6 the Minimize/Restore would free a couple megs. .NET definitely takes a lot more RAM too. In fact, an app I recoded for .NET takes about 10 megs more RAM just to start it. It's definitely a hog.

 

Hopefully someone can suggest some things.

mov ax, 13h

int 10h

  • 3 months later...
Posted
I am also having the same sort of problem. Applications seem to start off very large... (15mb and 30mb) once they get minimized and restored. The memory usage goes to 1mb and 20 mb.... Which would seem to be more reasonable. Any thoughts on why this occurs or how to prevent?
Posted
Has anyone tried useing the .NET Profiler API to clean up code? I'm just wondering.
Being smarter than you look is always better than looking smarter than you are.
  • *Gurus*
Posted

The garbage collector in .NET is inherently lazy. This is its best characteristic. Yes, you heard me correctly-- its best characteristic. If the collection algorithm constantly caused memory to be freed, you see a significant decrease in performance of your application. Instead, the GC frees memory only when it needs to be freed and I assure you that's not as often as one would think. If you have 3 .NET applications taking up 60MB of memory on a system with 256MB (which is measly nowadays), I don't really see the problem, as there isn't one. If other applications need memory the GC will free up what is needed and that's the end of it.

 

As it stands now I have Mozilla, Internet Explorer and Microsoft Document Explorer open on my system. Each of them is taking up 25MB+. Does it matter? Not at all, and they're not even running on .NET. The point is these applications are taking up the same amount of memory as your .NET applications-- and more importantly vice-versa.

Posted
.Net is slightly larger then Native Apps though because of the 3 background threads maintaining the App(Compiler Services)
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted
Well what Derek says is true, when I started using .NEt I noticed this problem too, posted around several forums asking for an answer, finally my app was finished, had to install it on several computers around the office, Windows 98, Windows Me, Windows XP, some of the computers with 64Mb and others with 256, and the GC worked as Derek says... freeing memory as needed, if you rcimputer has 128Mb free of memory .NET will use as much as possible, but when word, Excel or any other programs starts and ask for memory the GC does the trick. Try it.
Fat kids are harder to kidnap
Posted

Well after looking into this more. On other forums, it was indicated that the reason a minimize indicates it reduces the amount of memory is that the apps gets swapped out to disk and it just looks like it uses less memory from the task manager. If this is true, then I am basing how much memory is used on faulty spy glass...

 

But for my app the real problem I am trying to get a handle on is that after 3 - 24 hours of operation. The app dies. Either just disappears from the Task Manager or Gets a Stack overflow exception... The memory usage keeps growing and I wanted to get control of that.... I have not yet found the reason.

Posted

well im not sure if i can post it i got a simple function in which it pushes the memory down so much by 30 megs or so

 

at least...

 

the reason why simple forms that dont do anything use so much memory is becoz

 

the more memory u have the more .net apps reserve it, its not in use or anything if u monitor and open up like 50 forms it will use lesser and lesser memory...

 

same to if u ran the 35 meg memory usage app on a 64 meg PC it will use only 1 meg or less.. becoz there isnt enough memory

  • 1 month later...
Posted

Possible Solution...

 

I set up a test program to troubleshoot this problem. All I did was put a datagrid and two command buttons on a form. I then created a sqldataadapter and a dataset. I filled the dataset and then binded it to the datagrid with 67000 test records. Memory would keep increasing even though I would clear the dataset, accept changes, set both the data adapter and dataset to nothing, and call the dispose events for each. What finally freed the memory was setting the datasource for the datagrid to nothing. I didn't have to call the gc.collect. The datasource was being held in memory because of the datagrid reference?

I hope this helps...

Posted

Hi folks,

another thing to look out for is that if you declare an object within a method that is only used in that method, the garbage collector does not seem to free it until our application ends, thus beware and tidyup objects/memory as you go.

 

Calling of GC.Collect() as you go han help as well but I have read that it is not always recommended to do that. Does anybodyhave experience with any problems this may cause.

 

Does anybody know if the GC optimizations using generations works as it is supposed to or is it worth the effor if performance is not critical.

Hamlet
  • 1 year later...
Posted

solution

 

I found this, but the guy who wrote it doesn't reccomend it and he says that it is not necesary, but I don't believe him.

 

 

/*************************** minimize memory usage ***************************/

 

public class WorkingSetModerator : MarshalByRefObject

{

 

 

public WorkingSetModerator(diag.Process targetProcess, int proposedMaximum)

{

_targetProcess=targetProcess;

_memoryWatermark=proposedMaximum;

_targetProcess.MaxWorkingSet = new IntPtr(_memoryWatermark);

}

public WorkingSetModerator(int proposedMaximum) : this(diag.Process.GetCurrentProcess(), proposedMaximum) {}

private int _memoryWatermark;

private Thread _workerThread;

private diag.Process _targetProcess;

private time.Timer _timer;

private static object syncRoot=new object();

private void pollingMethod(object sender, time.ElapsedEventArgs e)

{

 

try {

lock(syncRoot)

{

if(_workerThread==null || ((int)(_workerThread.ThreadState & (ThreadState.Unstarted | ThreadState.Stopped)))>0)

{

_workerThread=new Thread(new ThreadStart(ReduceWorkingSet));

_workerThread.IsBackground=true;

_workerThread.Priority = ThreadPriority.BelowNormal;

_workerThread.Start();

}

}

} catch (Exception) {}

}

private double _interval=1200;

public double Interval

{

get

{

if(_timer!=null && _interval!=_timer.Interval)

_interval=_timer.Interval;

return _interval;

}

set

{

_interval=value;

if(_timer!=null && _interval!=_timer.Interval)

_timer.Interval=value;

}

}

public void ReduceWorkingSet()

{

try {

if(_targetProcess!=null && !_targetProcess.HasExited)

{

if(_targetProcess.WorkingSet>=_memoryWatermark)

_targetProcess.MaxWorkingSet = new IntPtr(_memoryWatermark);

}

} catch (Exception) {}

}

 

public void Start()

{

_timer=new time.Timer();

_timer.AutoReset=true;

_timer.Interval = _interval;

_timer.Elapsed +=new System.Timers.ElapsedEventHandler(pollingMethod);

_timer.Start();

 

}

public void Stop()

{

if(_timer!=null)

{

_timer.Stop();

_timer=null;

}

}

}

/*************************** minimize memory usage ***************************/

 

void Main() {

try {

WorkingSetModerator mod = new WorkingSetModerator(2000000);

mod.ReduceWorkingSet();

mod.Start();

} catch (Exception) {}

 

blabla...

}

Posted
This topics pretty old, but just a note, it's never good with trying to decrease the mem size, it's really not using that much memory, it's pre-allocated memory, someone explained it to me, the reason for pre-allocation of such a large chunk of memory, is because creating Objects on a managed heap is more efficient than creating it on the unmanaged heap on the memory.
Posted

I don't care about eficiency in my application; it's working with the disk so you don't notice that. I just don't want that somebody runs it looks in the task manager and sees 50 MB :mad: And with this he will see 4 MB.

 

Why isn't it a good idea to do that ? Can it crash ?

 

 

 

 

This topics pretty old' date=' but just a note, it's never good with trying to decrease the mem size, it's really not using that much memory, it's pre-allocated memory, someone explained it to me, the reason for pre-allocation of such a large chunk of memory, is because creating Objects on a managed heap is more efficient than creating it on the unmanaged heap on the memory.[/quote']

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...