1

Can I obtain how much memory is available (roughly) before a malloc()? I need to adjust some internal variables before doing a malloc()/new because performing a malloc() and then checking and deleting is not feasible.

(FYI, I am asking about glibc and tcmalloc's malloc(), but any other malloc() is also OK)

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
walkerlala
  • 1,599
  • 1
  • 19
  • 32
  • 1
    have a look here: https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process – Federico Aug 27 '18 at 10:31
  • 2
    What is your operating system? On Linux and Windows, there are many previous questions such as this: https://stackoverflow.com/questions/2513505/how-to-get-available-memory-c-g/26639774 – VLL Aug 27 '18 at 10:33
  • [sysinfo()](http://man7.org/linux/man-pages/man2/sysinfo.2.html).freeram – KamilCuk Aug 27 '18 at 10:43
  • 1
    This depends on your OS, there is no standard library function for it. – mnistic Aug 27 '18 at 11:15
  • Possible duplicate of [How to know the available virtual memory inside an application](https://stackoverflow.com/questions/28497840/how-to-know-the-available-virtual-memory-inside-an-application) – too honest for this site Aug 27 '18 at 11:57
  • 1
    Even if you could, how do you know the same memory is still available when you `malloc`? Just try to allocate what you need. You have to check the result anyway. Oh, and why is this tagged with two different languages? – too honest for this site Aug 27 '18 at 11:58
  • 4
    That's an XY problem. If your current design has to check in advance, it's wrong; change it. – too honest for this site Aug 27 '18 at 12:04

0 Answers0