小编mic*_*ael的帖子

了解glibc malloc修剪

我目前正在处理的一些程序消耗的内存比我想象的要多得多.所以我想了解glibc malloc修剪是如何工作的.我写了以下测试:

#include <malloc.h>
#include <unistd.h>

#define NUM_CHUNKS 1000000
#define CHUNCK_SIZE 100

int main()
{
    // disable fast bins
    mallopt(M_MXFAST, 0);

    void** array  = (void**)malloc(sizeof(void*) * NUM_CHUNKS);

    // allocating memory
    for(unsigned int i = 0; i < NUM_CHUNKS; i++)
    {
        array[i] = malloc(CHUNCK_SIZE);
    }

    // releasing memory ALMOST all memory
    for(unsigned int i = 0; i < NUM_CHUNKS - 1 ; i++)
    {
        free(array[i]);
    }

    // when enabled memory consumption reduces
    //int ret = malloc_trim(0);
    //printf("ret=%d\n", ret);

    malloc_stats();

    sleep(100000);
} …
Run Code Online (Sandbox Code Playgroud)

linux malloc free glibc

5
推荐指数
1
解决办法
1510
查看次数

获取时间戳的最快方法

我正在实现一些数据结构,其中我需要在一段时间后使某些条目无效,因此对于每个条目,我需要维护其插入时间戳。当我得到一个条目时,我需要再次获取时间戳并计算从插入开始的经过时间(如果它太旧,我无法使用它)。

许多线程高度满足此数据结构,因此我必须以最有效的方式获取此时间戳(oninsertfind)。效率在这里非常重要。

如果重要的话,我正在使用 C++ 开发的 linux 机器上工作。检索时间戳的最有效方法是什么?

顺便说一句,在我正在做的一些旧项目中,我记得我看到了一些直接从 CPU 获取时间戳的汇编命令(不记得命令了)。

c++ linux time cpu-registers

3
推荐指数
1
解决办法
3021
查看次数

标签 统计

linux ×2

c++ ×1

cpu-registers ×1

free ×1

glibc ×1

malloc ×1

time ×1