0

我有这个代码:

#include <stdio.h>
#include <unistd.h>
#include <json/json.h>
#include <stdio.h>

json_object *new_array;

void add_result_event(json_object *scan_result) {
    json_object_array_add(new_array, scan_result);
}


void add_scan_result() {
    json_object *scan_result = json_object_new_object();
    json_object_object_add(scan_result, "ssid", json_object_new_string("ssssssssssssssssssssssssssssiiiiiiiiiiiidddddddddd"));
    add_result_event(scan_result);
}

void alloc_object() {
    int i = 0;
    while (i < 100000) {
        add_scan_result();
        i++;
    }
}

int main() {
    new_array = json_object_new_array();
    printf(" not started.. see memory\n");
    system("ps -aux | grep sonj");
    sleep (1);

    alloc_object();
    printf(" allocated.. see memory\n");
    system("ps -aux | grep sonj");
    sleep (1);

    json_object_put(new_array);

    printf(" freed.. see memory\n");
    system("ps -aux | grep sonj");
    sleep (1);

    new_array  =  json_object_new_array();
    printf(" allocated.. see memory\n");
    alloc_object();
    system("ps -aux | grep sonj");
    sleep (1);
}

编译:

~$ gcc json_array_obj_d.c -ljson-c -o sonj

然后,运行:

~$ ./sonj

输出的 VSZ 字段未显示在json_object_put call.

所以释放json_object_put不给系统内存,但下一次分配也不会给系统更多的内存。

json_object_put当系统中出现空闲内存时调用After ?

4

0 回答 0