Stelin Posted February 7, 2003 Posted February 7, 2003 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.... ;) ;) Quote
Moderators Robby Posted February 7, 2003 Moderators Posted February 7, 2003 I started a new thread for you, the other one was 2 months old. Quote Visit...Bassic Software
Threads Posted February 7, 2003 Posted February 7, 2003 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. Quote mov ax, 13h int 10h
nablodr Posted June 6, 2003 Posted June 6, 2003 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? Quote
aewarnick Posted June 6, 2003 Posted June 6, 2003 Interesting, I am curious to see how this thread turns out. Quote C#
Mothra Posted June 6, 2003 Posted June 6, 2003 Has anyone tried useing the .NET Profiler API to clean up code? I'm just wondering. Quote Being smarter than you look is always better than looking smarter than you are.
*Gurus* Derek Stone Posted June 6, 2003 *Gurus* Posted June 6, 2003 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. Quote Posting Guidelines
aewarnick Posted June 6, 2003 Posted June 6, 2003 That's a releif! I was afraid I would have to switch. . . Quote C#
AndreRyan Posted June 7, 2003 Posted June 7, 2003 .Net is slightly larger then Native Apps though because of the 3 background threads maintaining the App(Compiler Services) Quote .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?
iebidan Posted June 10, 2003 Posted June 10, 2003 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. Quote Fat kids are harder to kidnap
nablodr Posted June 12, 2003 Posted June 12, 2003 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. Quote
Winston Posted June 14, 2003 Posted June 14, 2003 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 Quote
markb1719 Posted July 28, 2003 Posted July 28, 2003 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... Quote
donnacha Posted July 28, 2003 Posted July 28, 2003 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. Quote Hamlet
donnacha Posted July 28, 2003 Posted July 28, 2003 Hi folks, to monitor how mush memory is being used one could follow the use of PerfMon.exe described in the section "Monitoring Garbage Collections" of the article http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/TOC.ASP The outut is hard to follow but may be of some use to people. Quote Hamlet
hdagelicc Posted October 11, 2004 Posted October 11, 2004 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... } Quote
hdagelicc Posted October 11, 2004 Posted October 11, 2004 I forgot... using diag = System.Diagnostics; using System.Threading; using time = System.Timers; it is not VB but C# but you can rewrite it. Quote
Winston Posted October 11, 2004 Posted October 11, 2004 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. Quote
hdagelicc Posted October 13, 2004 Posted October 13, 2004 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'] Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.