Has anyone played around with REPORTing the memory (or displaying to terminal) using a CPP edit (see below)?
Specifically, the snippet appears to match the Windows-OS Task Manager reporting for Process Memory prior to `gc()` but:
```c++
#include <TMB.hpp>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#else
#include <sys/resource.h>
#include <unistd.h>
#endif
// Helper function to cout current memory usage in MB
void log_memory(std::string label) {
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
Rcout << label << ": " << physMemUsedByMe / 1024.0 / 1024.0 << " MB" << std::endl;
#endif
}
// Helper function to REPORT current memory usage in MB
double get_memory_MB() {
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS_EX pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc))) {
return (double)pmc.WorkingSetSize / 1024.0 / 1024.0;
}
#elif defined(__linux__)
// Linux-specific: Read from /proc/self/statm
// Returns (size resident share text lib data dirty) in pages
long pages = sysconf(_SC_PAGESIZE);
FILE* fp = fopen("/proc/self/statm", "r");
long rss = 0;
if (fp) {
if (fscanf(fp, "%*s%ld", &rss) == 1) { // Second value is RSS
fclose(fp);
return (double)rss * pages / 1024.0 / 1024.0;
}
fclose(fp);
}
#elif defined(__APPLE__)
// macOS-specific
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage) == 0) {
// ru_maxrss is in bytes on macOS
return (double)usage.ru_maxrss / 1024.0 / 1024.0;
}
#endif
return 0.0;
}
```
and then the standard template
```cpp
template<class Type>
Type objective_function<Type>::operator() ()
{
log_memory("Start of Template");
double memory_start_MB = get_memory_MB();
REPORT(memory_start_MB);
using namespace density;
[...]
}
```