• 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

    • "What's that? Microsoft recommends Intel CPUs? My next computer must have Intel then." - Nobody
    • Apple warns Australia against joining EU in mandating iPhone app sideloading by David Uzondu Apple has issued a warning to Australia, essentially telling the country not to follow the European Union's lead in making iPhone app sideloading a requirement. This communication comes as the Australian federal government considers new rules that could force Apple to open up its iOS ecosystem, much like what happened in Europe with recent legislation. Since iOS 17.4 and iPadOS 18, users in the EU have been able to get apps from outside the official App Store, a direct consequence of the DMA designating Apple as a "gatekeeper". The Australian government floated a proposal in a paper released late last year. The paper suggested "designating" digital platforms like Apple's App Store. Being designated this way means these platforms would have to follow new rules intended to keep them from limiting competition. The government pointed to Apple's in-app payment system, which usually comes with a commission, and the lack of sideloading as likely targets for regulation. Right now, apps like Netflix and Spotify can't let users subscribe through their iOS apps without giving Apple a big cut, and they're not even allowed to tell users where to find a better deal. Apple, in its response to this Australian paper, stated that Australia should not use the EU's Digital Markets Act "as a blueprint". The company's core argument is that the changes mandated by the EU's DMA, which came into full effect in March 2024, introduce serious security and privacy risks for users. Apple claims that allowing sideloading and alternative app stores effectively opens the door for malware, fraud, scams, and other harmful content. The tech company also highlighted specific concerns from its European experience, alleging that its compliance there has led to users being able to install pornography apps and apps that facilitate copyright infringement, things its curated App Store aims to prevent. Apple maintains that its current review process is vital for user protection, and that its often criticized 30% commission applies mainly to the highest earning apps, with most developers paying a lower 15% rate or nothing. It is worth noting that Apple's implementation of DMA requirements in the EU, which includes a "Core Technology Fee" for apps distributed outside its store, has already drawn scrutiny from European Commission officials who question if these measures truly comply with the DMA's idea of keeping the market fair. The Australian government has not yet detailed its next steps in this process, and the Treasury still needs to publish the full submissions to its proposal paper, including Apple's complete arguments. Source: The Guardian
    • Oh the Chinese crybabies who couldn't accept the fact that they lost GOTY to Astro Bot? Not interested
    • Download this SQL Essentials For Dummies eBook (worth $10) for free by Steven Parker FOR DUMMIES is a trademark of John Wiley & Sons, Inc. A right-to-the-point guide on all the key topics of SQL programming SQL Essentials For Dummies is your quick reference to all the core concepts of SQL—a valuable common standard language used in relational databases. This useful guide is straightforward—with no excess review, wordy explanations, or fluff—so you get what you need, fast. Great for a brush-up on the basics or as an everyday desk reference, this book is one you can rely on. Strengthen your understanding of the basics of SQL Review what you've already learned or pick up key skills Use SQL to create, manipulate, and control relational databases Jog your memory on the essentials as you work and get clear answers to your questions Perfect for supplementing classroom learning, reviewing for a certification, and staying knowledgeable on the job, SQL Essentials For Dummies is the convenient, direct, and digestible reference you've been looking for. Claim your complimentary eBook worth $10 for free, before the offer ends on June 17. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download the SQL Essentials For Dummies eBook (worth $10) for free Offered by Wiley, view other free resources The below offers are also available for free for a limited time: Winxvideo AI V3.0 Lifetime License for PC ($69.95 Value) FREE – Expires 6/8 Aiarty Image Enhancer for PC/Mac ($85 Value) FREE – Expires 6/8 Solutions Architect's Handbook, Third Edition ($42.99 Value) FREE – Expires 6/10 AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • No one is surprised. It goes both ways. Warhammer remake is not on the PS5, but Xbox/PC and Game Pass day 1 and it is NOT a Microsoft owned game (Sega). 100% Microsoft money deal. Timed of course. Sega wins because they get the Microsoft money now then when the deal ends they get the much larger PS audience to make money from. As far as Black Myth is concerned, the Chinese gaming platform market is very much PC then PS then everything else. Consoles were banned in China for years, then allowed and the PS is the console of choice. The release of Black Myth actually saw a PS5 console sale spike because of Chinese gamer's buying PS5's just to play this game. With the ever shrinking Xbox console market I can see a point where 3rd party games try to get Sony exclusive money because the ROI for Xbox console versions of those games is a shrinking endeavor.
  • Recent Achievements

    • One Month Later
      MoyaM earned a badge
      One Month Later
    • One Month Later
      qology earned a badge
      One Month Later
    • One Year In
      Frinco90 earned a badge
      One Year In
    • Apprentice
      Frinco90 went up a rank
      Apprentice
    • Week One Done
      theevergreentree earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      448
    2. 2
      +FloatingFatMan
      247
    3. 3
      snowy owl
      234
    4. 4
      ATLien_0
      200
    5. 5
      Xenon
      146
  • Tell a friend

    Love Neowin? Tell a friend!