3

Does C++ have method like in java

Runtime.getRuntime().totalMemory()
Runtime.getRuntime().freeMemory()

I am using in this way

long mem0 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
/*
stuff to do
*/ 
long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();              
System.out.println(mem1-mem0);

to find memory used by program. Is here any analogues in C++?

Aidos Askhatuly
  • 129
  • 1
  • 11
  • why do you need to know? Would using a simple memory profiler do the job or do you have some kind of runtime decisions to make based on that information? – Andreas Grapentin Apr 05 '13 at 12:20
  • there are some efficient memory managment tools for C++, like Valgring (Linux only). – lucasg Apr 05 '13 at 12:22
  • also, this is pretty much covered here: http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process – Andreas Grapentin Apr 05 '13 at 12:22
  • Check this out - http://stackoverflow.com/questions/8122277/getting-memory-information-with-qt – dtech Apr 05 '13 at 12:23
  • "Does C++ have method like in java" - BTW, Java do **not** have this methods. They are part of standard Java's library. Unfortunally C++ hasn't functionality you need in it's standard library – borisbn Apr 05 '13 at 12:24
  • @borisbn to nitpick, there's no "standard library". there is just a standard, and a couple of more or less standards compliant library implementations each with their couple of additions ;) – Andreas Grapentin Apr 05 '13 at 12:26
  • @AndreasGrapentin - isn't the STL a standard (template) library? Also this: http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library – dtech Apr 05 '13 at 12:31
  • @ddriver the term "standard library" is common, yes. but it does not refer to **the** standard library, but to a specific implementation of the standard. also, c++ "standard" library and stl are two different things :) – Andreas Grapentin Apr 05 '13 at 12:38
  • @ddriver for example, the glibc (one of the c libraries available for a couple of platforms) **does** include nonstandard extensions that allow extraction of memory stats. I was just objecting to "C++ hasn't functionality you need in it's standard library" because that is true for the standard, but not for the libraries. A slight difference, but, I think, a relevant one. – Andreas Grapentin Apr 05 '13 at 12:41
  • @AndreasGrapentin - there might be a lot of implementations, but there is a reason it is called a STANDARD library - it is a standard, even if the implementations are different they need to conform to the standard. And the STL is part of that standard library. – dtech Apr 05 '13 at 12:42
  • @ddriver generally that's true. But, as I said, different implementations of the standard come with different nonstandard extensions, that are, nonetheless, part of the library. And what makes you think the stl is part of the standard library? afaik, and wikipedia shares this opinion, "The C++ Standard Library is based upon conventions introduced by the Standard Template Library (STL). Although the C++ Standard Library and the STL share many features, neither is a strict superset of the other." – Andreas Grapentin Apr 05 '13 at 12:45
  • 1
    @AndreasGrapentin part of the library but not part of the standard library. On the rest you are correct, the SL and the STL are different entities. – dtech Apr 05 '13 at 12:47
  • @ddriver allright then. Let's agree to disagree :) Have a one-up! – Andreas Grapentin Apr 05 '13 at 12:48
  • @AndreasGrapentin. I have some stuff to do with the memory obtained in ruttime – Aidos Askhatuly Apr 05 '13 at 14:47

1 Answers1

2

Its not specified in the C++ standard, under Windows you need to use the WinAPI For example this and under linux you need to use a library or call syscalls.

Quonux
  • 2,975
  • 1
  • 24
  • 32