0

This code eats up to 1.5GB of memory and doesn't release it.

Why ? I'm expecting the memory to be freed after the call to the bigone function.

How to fix this ?

#include <iostream>
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/value.h>

typedef unsigned char utiny;

void watchmem() {
  char k;
  std::cout << "watch memory consumption then press a key ";
  std::cin >> k;
}

void bigone() {
  Json::Value conf = Json::arrayValue;
  for(utiny i = 0; i < 255; i++) {
    for(utiny j = 0; j < 255; j++) {
      for(utiny k = 0; k < 255; k++) {
        conf[i][j][k] = 0;
      }
    }
  }
}

int main(int argc, char **argv) {
  bigone();
  watchmem();
}

build:

g++ -std=c++11 -o chkmem chkmem.cpp -ljsoncpp && ./chkmem
lxndr
  • 11
  • 3
  • Very closely related: [Does calling free or delete ever release memory back to the “system”](http://stackoverflow.com/questions/1421491/does-calling-free-or-delete-ever-release-memory-back-to-the-system). – David Hammen Apr 05 '17 at 12:20
  • Assuming jsoncpp does things correctly, the dynamically allocated memory associated with the variable `conf` is deleted on the return from `bigone`. Deleting or freeing or dynamically allocated memory does not mean necessarily mean that that memory is released to the "system". Releasing memory back to the system does not happen in many operating systems. The OS instead takes care of this issue virtually. – David Hammen Apr 05 '17 at 12:27
  • @DavidHammen thanks for your comments. In my real case scenario, the linux kernel ends up killing one of the processes, so I suppose it doesn't see the memory as potentially available. – lxndr Apr 05 '17 at 13:18

0 Answers0