DCSIMG
.Net Memory Problems - Yoav Blob

Yoav Blob

.Net Memory Problems

לאחרונה נתקלתי בתופעה מוזרה, פתחתי אפליקצית .net נקיה וראיתי שהיא תופסת ~8MB. לאחר מכן השתמשתי במחלקות סטנדרטיות של .net והזיכרון שלי הגיע ל20MB.

התחלתי לנסות לברר למה זה והבנתי שמדובר בטעינה של הAssemblyים של .net.
אבל עדיין זה נראה לא סביר שאפליקציה לא משחררת את הזיכרון לאחר שהיא טוענת אותו.

לאחר חיפוש באינטרנט ראיתי את המאמר הבא: Minimize .NET Memory Consumption

המאמר מתאר שימוש בWinAPI לצורך צמצום הזיכרון שמוקצא לאפליקציה ע"י הwindows.

מה שבופעל קורה זה שבתהליך טעינת הAssembly הwindows מקצה הרבה זיכרון לאפליקציה, אבל לאחר השימוש בזיכרון הוא לא מוחזר למערכת הפעלה אלא נשאר אצל האפליקציה למקרה שהיא תצטרך אותו. בשביל שהwindows יקח בחזרה את הזיכרון נבצע קריאה למתודה: SetProcessWorkingSetSize, לפי תיעוד של מיקרוסופט שליחה של -1 יגרום לפעולת הTrim הרצויה.

להלן הקוד המלא:

 

[DllImport("kernel32.dll")]

static extern bool SetProcessWorkingSetSize(IntPtr hProcess, IntPtr

dwMinimumWorkingSetSize, IntPtr dwMaximumWorkingSetSize);

private static void FlushMemory()

{

GC.Collect();

GC.WaitForPendingFinalizers();

SetProcessWorkingSetSize(

Process.GetCurrentProcess().Handle, new IntPtr(-1), new IntPtr(-1));

}

תוכן התגובה

Shimon krohkmal כתב/ה:

hi,

this could be useful if your application experiencing memory problems (say process , gets near 2GB)

otherwise it's not recommended doing so.

this operation is pretty expensive and whenever you would want to use those "preloaded" classes, the application would have to "reload" it back again.

btw, if my memory does not fools me, there is a registry key that you can set  on your machine to cause the same effect.

# September 30, 2007 11:03 PM

Nati Dobkin כתב/ה:

Hi Shimon,

You have a point in what you say but as the guy from the article that Yoav mentioned said that he reduce the memory amount from 60M up to 12M (bout 80% improvement) - that's a lot! No mater how much memory your app takes.

Another thing C++ forms app with single window takes no more then 1M memory and same app written at c# takes up to 20M. if you reduce that 80% you get almost the same amount of memory - which is now something that you can work with.

# October 1, 2007 12:40 AM

Moshe L כתב/ה:

מעניין מאוד.

תמיד סבלתי מזה שאפליקציות דוט נט מגיעות מהר מאוד לניצולות זיכרון איומות לעומת אפליקציות שכתובות ב-VB 6 למשל, או ++C.

ישר כח !

# October 1, 2007 11:54 AM

Justin-Josef Angel [MVP] כתב/ה:

חס וחלילה לקרוא ל-

GC.Collect!

כמו שאמרו לפני, מדובר בפעולה שלוקחת המון מעבד והמון זמן.

במקום לעבוד עם כל מיני מתודות מוזרות במערכת ההפעלה עדיף בחום להתשמש במקבילות הדוט-נטיות שלהן:

blogs.microsoft.co.il/.../Question-from-.Net-Tapuz-forum_3A00_-How-to-limit-the-memory-_2800_RAM_2900_-my-Winforms-application-uses_3F00_.aspx

# October 1, 2007 5:12 PM

Yoav michaeli כתב/ה:

התחליף ה.net לא מבצע אותו הדבר.

שים לב שאני מבצע trim לזיכרון ולא מגביל אותו לגודל מסיום.

מה שגורם לזה שבמידה והאפליקציה תצטרך את הזיכרון היא תקח אותו מהwindows בחזרה.

# October 1, 2007 8:31 PM

Shimon krohkmal כתב/ה:

@Justin,

the one that Yoav, and the one you present are two different method by purpose.

regarding GC.Collect(),

when working with 3rd party objects(and with much luck, they will contain an unmanaged core, like sharepoint for example), calling that method is a must.

@Nati,

i have no argue with that, it's really lowers the memory  usage.

the real question is, should you do it .

if you remember, a simple hello world compiled with borland c++ would take about 8K,

whereas compiling the same program with visual studio would have blown the size of it to 200K and sometimes more.

it's a question of how it's compiled and what are the "extra stuff" that consumes memory.

the bigger issue is weather it's addition provides us a greater performance or damage it ?

in most cases(specially in enterprise applications) the best thing to do is to leave it there .  

# October 1, 2007 8:34 PM

Adi כתב/ה:

What your code is doing is twiking the size of the working set for your application, as oppose to actually changing the amount of memory it requires.

# October 5, 2007 1:06 AM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 7 and 6 and type the answer here:


Enter the numbers above: