Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exiting gracefully when memory exhausted
Forum Updated to NodeBB v4.3 + New Features

Exiting gracefully when memory exhausted

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 908 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JoeCFDJ JoeCFD

    One way to avoid this is to query available memory before any big block memory is allocated.

    W Offline
    W Offline
    wrosecrans
    wrote on last edited by
    #3

    @JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.

    Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.

    JoeCFDJ AndyBriceA 2 Replies Last reply
    1
    • W wrosecrans

      @JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.

      Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #4

      @wrosecrans Agree. Add some factor to the available memory while it may be hard to get the exact number of free memory.

      1 Reply Last reply
      0
      • W wrosecrans

        @JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.

        Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.

        AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by
        #5

        @wrosecrans said in Exiting gracefully when memory exhausted:

        @JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.

        Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.

        I have considered allowing the user to set a maximum memory usage and exit gracefully if they hit that limit. But the current appoach seems to work fine on macOS, I'm surprised it doesn't work on Windows.

        W J.HilkJ 2 Replies Last reply
        0
        • AndyBriceA AndyBrice

          @wrosecrans said in Exiting gracefully when memory exhausted:

          @JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.

          Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.

          I have considered allowing the user to set a maximum memory usage and exit gracefully if they hit that limit. But the current appoach seems to work fine on macOS, I'm surprised it doesn't work on Windows.

          W Offline
          W Offline
          wrosecrans
          wrote on last edited by
          #6

          @AndyBrice Don't be surprised if it starts to fail visibly on MacOS after an OS update, a new Qt version, or some changes to your code. It "appears" to work, but you can't rely on the behavior you are seeing.

          AndyBriceA 1 Reply Last reply
          0
          • W wrosecrans

            @AndyBrice Don't be surprised if it starts to fail visibly on MacOS after an OS update, a new Qt version, or some changes to your code. It "appears" to work, but you can't rely on the behavior you are seeing.

            AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote on last edited by
            #7

            @wrosecrans You might be right. I'm just surprised there doesn't seem to be a reliable way to handle this.

            I don't suppose Qt has a crossplatform way to find out how much memory a process is using? That would be useful.

            1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #8

              I'm not aware of Qt providing anything that fits the bill.

              Providing a single useful memory usage number from an application's perspective sounds challenging. Shared pages, mapped but unallocated pages, buffers held on behalf of the process, etc make the analysis complicated.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              AndyBriceA 1 Reply Last reply
              1
              • AndyBriceA AndyBrice

                @wrosecrans said in Exiting gracefully when memory exhausted:

                @JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.

                Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.

                I have considered allowing the user to set a maximum memory usage and exit gracefully if they hit that limit. But the current appoach seems to work fine on macOS, I'm surprised it doesn't work on Windows.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #9

                @AndyBrice said in Exiting gracefully when memory exhausted:

                I'm surprised it doesn't work on Windows

                I‘m surpised as well, maybe its msvc‘s exception handling? I remember that to be bad or haphazardly implemented. Try with nothrow and check against nullptr instead.

                I guess you know how to override new/delete to use a custom memory pool ? I fyou decide to go the fixed max memory route


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                AndyBriceA 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @AndyBrice said in Exiting gracefully when memory exhausted:

                  I'm surprised it doesn't work on Windows

                  I‘m surpised as well, maybe its msvc‘s exception handling? I remember that to be bad or haphazardly implemented. Try with nothrow and check against nullptr instead.

                  I guess you know how to override new/delete to use a custom memory pool ? I fyou decide to go the fixed max memory route

                  AndyBriceA Offline
                  AndyBriceA Offline
                  AndyBrice
                  wrote on last edited by
                  #10

                  @J-Hilk

                  Try with nothrow and check against nullptr instead.

                  That would involve a lot of rewriting!

                  I guess you know how to override new/delete to use a custom memory pool ?

                  Sounds nasty...!

                  J.HilkJ 1 Reply Last reply
                  0
                  • jeremy_kJ jeremy_k

                    I'm not aware of Qt providing anything that fits the bill.

                    Providing a single useful memory usage number from an application's perspective sounds challenging. Shared pages, mapped but unallocated pages, buffers held on behalf of the process, etc make the analysis complicated.

                    AndyBriceA Offline
                    AndyBriceA Offline
                    AndyBrice
                    wrote on last edited by
                    #11

                    @jeremy_k The Windows or Mac task manager shows the amount of memory a process is consuming. That is the number I want.

                    1 Reply Last reply
                    0
                    • AndyBriceA AndyBrice

                      @J-Hilk

                      Try with nothrow and check against nullptr instead.

                      That would involve a lot of rewriting!

                      I guess you know how to override new/delete to use a custom memory pool ?

                      Sounds nasty...!

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #12

                      @AndyBrice

                      That would involve a lot of rewriting!

                      you do not have to necessarily rewrite your whole code, a small test program could, as first step be enough, no ?

                      Sounds nasty...!

                      I've never done it myself, and only seen it done once, in the source code of Command & Conquer


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved