• 0

Reduce .NET app RAM usage


Question

Recommended Posts

  • 0

You should leave the .NET runtime do the job. It will reduce the working sets automatically when memory runs out globally. I don't know why everyone is so obsessed with the memory column on taskman or perfmon with .NET apps. That counter is not representative for the .NET applications memory usage.

Other than that it will require you to import a function call from ntdll.dll or something. Winston and bangbang23 can tell you exactly how.

  • 0

Ahh gees let me reiterate myself for the 500000000 time, but it's alright, you see the .NET Framework, does the memory allocation, what the framework does is, when your system has alot of memory free, say for example you have 512 megs of RAM and use up only 100 then your app with just one form with nothing probably uses about 25 megs, this is primarily because the more RAM you have the more .NET apps reserves for itself, i duno the real reason why it does this it was so confusing listening to the architect guy talk in germanglish lol... but yeah, so say for example if your system had 128 megs of ram and you got 28 megs spare, then your .net app will only make out about 300k - 800k, this is the single form that has nothing on it.

Let's just say what you see in the task manager is probably deceiving, theoretically the application just runs at a very small amount of memory but what's shown for the memory is how much the application has allocated for itself, as soon as the system runs low on memory the .net app will decrease in memory usage, some computer illiterate people might look at the task manager and sh*t their pants seeing your app using so much, so below is the fix which will practically just refresh the task manager so that it displays the memory that's REALLY in use and remove the memory that's spare

so you can come to a conclusion to say what's shown in task manager is basically the representation of:

memory actually being used + additional memory reserved for the program = task manager display

applying of the following function:

? ?
Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Int32, ByVal dwMaximumWorkingSetSize As Int32) As Int32
 ? ?'Call the Kernel API Set process memory size

Public Function SaveMemory() As Int32

 ? ? ? ?Return SetProcessWorkingSetSize(Diagnostics.Process.GetCurrentProcess.Handle, -1, -1)

 ? ?End Function

'Call the function in the appropriate events, i.e. form Activated, Form Deactivated, which a friend of mine recommeneded me doing

SaveMemory()[b]displays only memory actually consumed by app[/b]by app

I hope this has cleared it up a bit, and could some Mod just sticky this, it's been asked alot!

Cheers

  • 0

Note, however, that there are side effects to using SetProcessWorkingSetSize. If you call that API regularly and the Framework needs to reduce the working size of the app for some reason, the app will throw an OutOfMemoryException, whether the system is actually out of memory or not.

And, as Winston said, that API is just a convenient illusion for people with a gigabyte of RAM who still feel they need to guard every byte jealously. It doesn't actually accomplish anything.

--

Danny Smurf

  • 0

i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management :)

  • 0
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management :)

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

  • 0
  session? said:
Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Int32, ByVal dwMaximumWorkingSetSize As Int32) As Int32
  weenur said:
[DllImport("kernel32.dll")]

public extern static bool SetWorkingProcessSetSize( IntPtr hProcess, int min, int max );

Returns Int32, not bool...

  • 0
  bangbang023 said:
trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

If I'm ever going to release one of my apps into public, I will make sure that it has a dialog that shows up on first run that explains them CLEARLY how .NETs memory management works, including checkbox to agree not to complain! Better way than ****ing up an applications performance and stability.

  • 0
  frogg said:
  weenur said:
[DllImport("kernel32.dll")]

public extern static bool SetWorkingProcessSetSize( IntPtr hProcess, int min, int max );

Returns Int32, not bool...

the C declaration of SetProcessWorkingSetSize:

BOOL SetProcessWorkingSetSize( HANDLE hProcess, SIZE_T dwMinimumWorkingSetSize, SIZE_T dwMaximumWorkingSetSize)

You can use bool for BOOL in interop.

I inadvertently swapped Process with Working. That I will admit to being incorrect on. :D

  • 0
  bangbang023 said:
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management  :)

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

I have released the app (its not public stuff, its for internal stuff in my company) and noone cares - i don't think they'd ever notice.

If people are just going on whats showing up in task manager then, well, a) they don't know what they're talking about, and b) why are they staring at task manager anyway?

  • 0
  nowimnothing said:
  bangbang023 said:
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management  :)

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

I have released the app (its not public stuff, its for internal stuff in my company) and noone cares - i don't think they'd ever notice.

If people are just going on whats showing up in task manager then, well, a) they don't know what they're talking about, and b) why are they staring at task manager anyway?

I'm not saying those people are right, but I have had users complain because "it's a sticky note program and shouldn't be using 15mb of RAM." I basically had to do it or be faced with losing users. I don't make money off my app, but I still don't want to lose users.

Tom: First off, it's nice having a somewhat normal, non-fighting discussion lol. Second, I have not had any problems with stability or performance with my app.

  • 0
  bangbang023 said:
  nowimnothing said:
  bangbang023 said:
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management?:))

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

I have released the app (its not public stuff, its for internal stuff in my company) and noone cares - i don't think they'd ever notice.

If people are just going on whats showing up in task manager then, well, a) they don't know what they're talking about, and b) why are they staring at task manager anyway?

I'm not saying those people are right, but I have had users complain because "it's a sticky note program and shouldn't be using 15mb of RAM." I basically had to do it or be faced with losing users. I don't make money off my app, but I still don't want to lose users.

Tom: First off, it's nice having a somewhat normal, non-fighting discussion lol. Second, I have not had any problems with stability or performance with my app.

You'll understand that it may have adverse effects which is unpredictable at times, once again a friend of mine taught me programming, and he has brought me up the right way in thinking, and bangbang i hope you begin to take account for your applications performance, not criticising you much but like i always stressed to u on MSN you shouldnt care about if the thing can be implemented, it should be done in a way that it wont drastically effect performance on ur application, which i did back then wen i designed my app because i had a time constraint as it was a assignment and i needed to get it over and done with, but once i finish my exams im gonna learn .net from the ground up.

  • 0
  Winston said:
You'll understand that it may have adverse effects which is unpredictable at times, once again a friend of mine taught me programming, and he has brought me up the right way in thinking, and bangbang i hope you begin to take account for your applications performance, not criticising you much but like i always stressed to u on MSN you shouldnt care about if the thing can be implemented, it should be done in a way that it wont drastically effect performance on ur application, which i did back then wen i designed my app because i had a time constraint as it was a assignment and i needed to get it over and done with, but once i finish my exams im gonna learn .net from the ground up.

I've told you a million times before, I get the basic code layed out then I worry about performce. It can't run well if it don't run at all. My code is definitely not the best, but neither is yours or anyone else's here, cause otherwise we'd be very rich, but my code is descent (lacking comments but not a problem for me.) Anything that works on my program works well and does not slow down the program. I wouldn't include a "feature" if it slowed down the proggy.

Either way, this is off topic. You don't like my code? So be it. I honestly think you want it done your way or no way at all, but there are plenty ways to accomplish something.

I'll way for Tom to reply so we can continue the discussion we were having.

  • 0

Back to SetProcessWorkingSetSize...

As far as losing users, I can understand where you're coming from, bangbang. I went through that dilemma myself. In some cases, it's appropriate to use the API and shut them up, and in some cases it's not.

For a non-trivial app, it's definitely not worth using. If you're creating (for example), a productivity app, the memory footprint isn't so important. A word processor, email client, InfoPath-type app or whatever... people really aren't going to care if it takes 50MB. On the other hand, for a trivial app (a sticky note program, a registry startup watcher, that sort of power-tool type of stuff), people WILL care, because they've got the idea stuck in their head that minor apps should be minor RAM users. That's not the right attitude, of course, but when you're making these kinds of apps, your userbase is quite a bit different. You get every johnny-knowitall from all over the Internet critiquing your app.

If you're charging for your app (regardless of what type it is), it's definitely not a good idea to use that API. Side effects like those **** people off. And ****ing off paying customers is an entirely different thing from ****ing off freebie hunters.

--

Danny Smurf

  • 0
  bangbang023 said:
I've told you a million times before, I get the basic code layed out then I worry about performce. It can't run well if it don't run at all. My code is definitely not the best, but neither is yours or anyone else's here, cause otherwise we'd be very rich, but my code is descent (lacking comments but not a problem for me.) Anything that works on my program works well and does not slow down the program. I wouldn't include a "feature" if it slowed down the proggy.

I think you have a correct approach. Optimization is always something you do last, and judiciously. As Kestrel has correctly stated, as have many others, cpu cycles(and memory) are cheaper than man-hours.

@dannysmurf

I agree 100%. End users suck. ;)

  • 0
  dannysmurf said:
Back to SetProcessWorkingSetSize...

As far as losing users, I can understand where you're coming from, bangbang. I went through that dilemma myself. In some cases, it's appropriate to use the API and shut them up, and in some cases it's not.

For a non-trivial app, it's definitely not worth using. If you're creating (for example), a productivity app, the memory footprint isn't so important. A word processor, email client, InfoPath-type app or whatever... people really aren't going to care if it takes 50MB. On the other hand, for a trivial app (a sticky note program, a registry startup watcher, that sort of power-tool type of stuff), people WILL care, because they've got the idea stuck in their head that minor apps should be minor RAM users. That's not the right attitude, of course, but when you're making these kinds of apps, your userbase is quite a bit different. You get every johnny-knowitall from all over the Internet critiquing your app.

If you're charging for your app (regardless of what type it is), it's definitely not a good idea to use that API. Side effects like those **** people off. And ****ing off paying customers is an entirely different thing from ****ing off freebie hunters.

--

Danny Smurf

I agree with you fully. My app has, in my mind, the nicest set of features for a note program (maybe lacking a few areas but it's only on 0.52). Either way, if people were going to pay for this and I knew they wouldn't nitch and moan, I would remove the call to that API in a flash, but, being a free program, I had nothing else to offer except the low ram usage selling point. I can't afford benefits or anythign like full 24/7 tech support lol.

  • 0

Well, what about an option based on my dialog proposal?

Make a dialog that shows up on first run that explains the memory management **** in easy words and then two radio buttons that allow you to choose to use the "memory preserving" option (SetProcessWorkingSetSize) or not.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • SeaMonkey 2.53.21 by Razvan Serea The SeaMonkey project is a community effort to develop the SeaMonkey all-in-one internet application suite. Such a software suite was previously made popular by Netscape and Mozilla, and the SeaMonkey project continues to develop and deliver high-quality updates to this concept. Containing an Internet browser, email & newsgroup client with an included web feed reader, HTML editor, IRC chat and web development tools, SeaMonkey is sure to appeal to advanced users, web developers and corporate users. Under the hood, SeaMonkey uses much of the same Mozilla source code which powers such successful siblings as Firefox, Thunderbird, Camino, Sunbird and Miro. Legal backing is provided by the Mozilla Foundation. SeaMonkey 2.53.21 changelog: Unable to load JSON Bookmarks file, Open/Cancel do the same thing bug 1940204. Move replaceVars helper into menu-manager.js for cZ bug 1937379. Remove dumpObject helper from utils.js in cZ bug 1937380. Remove toOpenWindowByType helper from utils.js in cZ bug 1937382. Fix makeLogName helper to not encode twice in prefs.js in cZ bug 1937387. Remove use of escapeFileName helper and tidy up pref_mungeName helper in cZ bug 1937395. Add helper to file-utils.js for ensuring an nsIFile is returned in cZ bug 1937397. Remove unused http.js file from cZ bug 1937890. Remove unused IRC tests from static.js in cZ bug 1937896. Switch from deprecated escape/unescape in cZ bug 1938933. Tidy up use of prefBranch outside of pref-manager in cZ bug 1938935. Make use of pref fallbacks in pref-manager in cZ bug 1938937. Remove unused edit context menu from cZ bug 1939929. Use XPCOMUtils.generateQI in connection-xpcom in cZ bug 1939930. Merge menus.xul, popups.xul and scripts.xul into chatzilla.xul bug 1939958. Make use of toSOutputStream and toSInputStream helpers in DCC code in cZ bug 1939965. Stop hard-coding commandkey for reloadui in cZ bug 1939968. Use suite's FillInHTMLTooltip helper instead of having own version in cZ bug 1939969. Split custom-away from other away/back commands in cZ bug 1942655. Remove ChatZilla Homepage link from Help menu and about command in cZ bug 1942916. Re-arrange toolbar menus in cZ bug 1943783. Remove ChatZilla Homepage link from about and prefs dialogs in cZ bug 1943844. Use custom controller for userlist and tidy up some controller use in cZ bug 1945325. Make use of observes for show/hide elements in cZ bug 1945378. Don't dynamically create focus-input key element in cZ bug 1947028. Remove unused toolbar creation code in cZ bug 1947030. Remove unused updateMenus code from cZ bug 1947031. Clean up whitespaces in cZ package manifest bug 1947040. Remove unused uninstallKeys code from cZ bug 1950002. Use node.remove(), especially instead of node.parentNode.removeChild(node) in cZ bug 1951250. Remove __cz_condition from cZ bug 1951253. Remove outputWindowURL pref from cZ bug 1951256. Remove unused JS tests in cZ bug 1951297. Use includes, startsWith and endsWith instead of indexOf and substr in cZ bug 1951302. Use {} and [] instead of new Object() and new Array() in cZ bug 1951303. Fixup function naming for lint in cZ bug 1955141. Fixup method naming for lint in cZ bug 1955767. Remove old Mozilla 1.0 code from updateAppMotif in cZ bug 1955771. Use throw Components.Exception in cZ bug 1955774. Migrate output-window from HTML to XHTML to make localisation more standard in cZ bug 1955825. Merge munger.js into mungers.js in cZ bug 1956373. Remove unused tagName from mungers.js in cZ bug 1956374. Flatten directory structure in cZ bug 1956375. Tidy up about dialog page in cZ bug 1956376. Add helper to commands.js for sending CTCP commands in cZ bug 1956377. Migrate to standard menus for menu toolbar in cZ bug 1957763. Install plugin dialog broken in cZ bug 1961599. cZ change nick menu not working bug 1962112. Away status isn't reflected correctly in all channels in cZ bug 1962234. Fix dark motif userlist in cZ bug 1967072. SeaMonkey Composer adds moz-do-not-send attribute for links and images bug 1827146. Use menu_Toolbars overlay for navigatorOverlay and console bug 1945335. Remove defunct 2.53 prerelease builds from debugQA extension bug 1947043. Handling of MOZ_LANGPACK_CONTRIBUTORS in defines.inc files should be less custom bug 1951101. Control + U shortcut for underlined text is not working bug 1872514. Context menu search (with default search engine) does not work in the content area of a message compose or SM-Composer window bug 1062092. The following bugs were fixed in our branch of the Gecko source code only: on FreeBSD sqlite3 fails to link for missing math functions bug 1944954. Expand init.configure to use version_package.txt to set MOZ_PKG_VERSION bug 1952757. Change supported msvc Compilers for SeaMonkey 2.53 bug 1954176. SeaMonkey 2.53.21 contains (among other changes) the following major changes relative to SeaMonkey 2.49.5: The Bookmarks Manager has switched its name to Library, and now also includes the History list. When History is invoked, the Library will be shown with the History list selected. The extensive modifications were needed because of Mozilla Gecko platform API changes. Download Manager has been migrated to a new API. Although it looks pretty much the same as before, the search option is missing and some other minor details work differently. The previous downloads history is removed during the upgrade. The layout panel was added to the CSS Grid tools. TLS 1.3 is the default SSL version now. Support for all NPAPI plugins like Flash, Java and Silverlight has been removed. For displaying pdf files in the browser you can use pdf.js-seamonkey from Isaac Schemm. SeaMonkey now uses a new api for formatting regional data like time and date. Default is to use the application locale of the current SeaMonkey build. If you use a language pack or a different OS formatting this is usually not desired. You can change the formatting from the application locale to the regional settings locale (OS) in the preferences dialog under "Appearance". SeaMonkey 2.53.21 uses the same backend as Firefox and contains the relevant Firefox 60.8 security fixes. Download: SeaMonkey 64-bit | Portable SeaMonkey 64-bit ~60.0 MB (Freeware) Download: SeaMonkey 32-bit | Portable SeaMonkey 32-bit View: SeaMonkey Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • It doesn't work for the view, but the music gets me every time:  
    • I remember when it first came about and the rubbish that was on it, and I did have a look now and again to see if it improved, and it really did not. I have never downloaded anything from it. Even on my Mac I tend to avoid the store, i prefer to download from source.
    • Are there any others that are as good?
    • Recent reviews very negative. I'll skip this one.
  • Recent Achievements

    • First Post
      Uranus_enjoyer earned a badge
      First Post
    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
    • Week One Done
      CHUNWEI earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      428
    2. 2
      +FloatingFatMan
      201
    3. 3
      snowy owl
      192
    4. 4
      ATLien_0
      187
    5. 5
      Xenon
      142
  • Tell a friend

    Love Neowin? Tell a friend!