0

Let's say I've have a resource intensive program that works better the more memory it has, up to the computers entire available memory. I want to be able to use all of the memory I can without stopping other programs on the computer from using memory if they need it.

To get the best of both worlds I want some way for my program to be signaled by the OS if another process wants memory and give me a chance to give it up gracefully and let both programs continue happily.

Is such a thing possible to achieve in c++? If so how can it be achieved (on both Windows and Unix systems)?

Edit:

Based on the given comments, it seems that the answer is a no, this is simply not possible exactly as stated, though a workaround is feasible using the methods from How to get memory usage at runtime using C++? to dynamically detect if the memory is running low and free memory accordingly to achieve similar (though not precisely the same) results.

rtpax
  • 1,687
  • 1
  • 18
  • 32
  • I'm getting a lot of downvotes here and no explanation on what's wrong with my question. Any input? – rtpax Dec 20 '18 at 22:15
  • On windows you could limit your working set size and let the OS handle swapping your process out to your page file when a different application needs ram. Applications are not going to ask your program for memory – drescherjm Dec 20 '18 at 22:16
  • 2
    In general, a program does not know about the operating system's allocation of memory. At least not in the language standard itself. And there is no mechanism in Windows or Linux for another process to say _"oi, greedy processes play nice for me"_. The best you could hope to achieve I think is to have your program attempt to monitor memory usage in the system and reduce usage after the fact. I'm not convinced this would be a good idea. – paddy Dec 20 '18 at 22:16
  • @drescherjm I'm aware. The question is about avoiding the limit in working size and when another program requests memory from the OS, having the OS let my program know – rtpax Dec 20 '18 at 22:17
  • @paddy I would assume that such a thing would not be in the language standard proper, but would be platform specific, if possible at all. Even a way to detect when available memory is getting low would partially solve the stated issue, which is definitely possible – rtpax Dec 20 '18 at 22:20
  • 1
    Your application can ask for the memory status at a regular interval and trim the working set size when some threshold is met. I don't know of any way to have the OS warn you. – drescherjm Dec 20 '18 at 22:20
  • 1
    ***Even a way to detect when available memory is getting low would partially solve the stated issue*** I believe you can find this out in both windows and linux through OS specific APIs. – drescherjm Dec 20 '18 at 22:20
  • 1
    Yes, it's platform-specific, and you can't always rely on numbers you get back, given the OS's ability to do virtual paging, compression, etc. – paddy Dec 20 '18 at 22:21
  • If you are on linux there are ways to check the memory consumption via the shell. https://www.binarytides.com/linux-command-check-memory-usage/ – bradgonesurfing Dec 21 '18 at 08:10
  • @rtpax You are getting downvotes but a similar question got 76 upvotes. I guess it depends on how you ask the question. If you had just asked a straight to the point question about accessing memory usage rather than details about your use case then maybe the downvoting would be different. – bradgonesurfing Dec 21 '18 at 08:12
  • Possible duplicate of [How to get memory usage at runtime using C++?](https://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-runtime-using-c) – bradgonesurfing Dec 21 '18 at 08:12
  • I'm surprised, I would have expected there to be platform specific equivalents to Java's weak_ptr, but I can't find anything like that. Seems like it would be useful. You can sort of emulate it with file mappings, I guess. – Mooing Duck Dec 21 '18 at 22:12
  • @MooingDuck I couldn't find anything searching for a java weak_ptr, only the c++ weak_ptr. Were you thinking of that or something else? – rtpax Dec 21 '18 at 22:17
  • Nevermind, I found it by the name WeakReference – rtpax Dec 21 '18 at 22:20
  • 1
    @paddy Are you sure there's no such mechanism on either Windows or Linux? I'm pretty sure Windows has several. [This](https://msdn.microsoft.com/en-us/library/Aa366541(v=VS.85).aspx) is one: "*Applications can use memory resource notification events to scale the memory usage as appropriate. If available memory is low, the application can reduce its working set. If available memory is high, the application can allocate more memory.*" There [have been ](https://lwn.net/Articles/267013/) proposals for similar functionality on Linux, but to my knowledge none is yet standard. – David Schwartz Dec 21 '18 at 23:48

0 Answers0