C++ linux x64 c++;为链表分配太多的内存;为什么?

C++ linux x64 c++;为链表分配太多的内存;为什么?,c++,memory-management,linked-list,C++,Memory Management,Linked List,我将使用stackoverflow上的链表示例代码来说明这个问题 我的C++编写的进程(x64)包含这个链表代码: 删除旧代码段;如果评论不再有意义,我很抱歉 添加了完整的工作代码以显示我的问题所在。 编译:g++linkedlist.cpp-o链接列表 #include <cstdlib> #include <iostream> using namespace std; struct node { public : unsigned

我将使用stackoverflow上的链表示例代码来说明这个问题

我的C++编写的进程(x64)包含这个链表代码:

删除旧代码段;如果评论不再有意义,我很抱歉

添加了完整的工作代码以显示我的问题所在。 编译:g++linkedlist.cpp-o链接列表

#include <cstdlib>
#include <iostream>

using namespace std;


 struct node
{
    public :
          unsigned int part1; // 4 bytes
          unsigned int part2; // 4 bytes
          node *next;         //pointer, 8 bytes on 64 bit system
      unsigned int read_part1();
 };


struct LinkedList
 {
     public:
     LinkedList();
          void insert(unsigned int data[], unsigned int data1);
          bool isEmpty() const;
          node* head;
 };

unsigned int node::read_part1() {
return part1;
}
 LinkedList::LinkedList():
 head(NULL)
{
}

bool LinkedList::isEmpty() const
{
  return (head == NULL);
}

  void LinkedList::insert(unsigned int data[], unsigned int data1)
 {



    node* oldHead = head;
    node* newHead = new node();
    newHead->part1 = data[0];
    newHead->part2 = data1;
    newHead->next = oldHead;
    head = newHead;

  }

unsigned int allocations = 300000000;
unsigned int index_size = 430000000;//index of lists, 430m,.
                    //will be creatad on heap
    LinkedList *list = NULL;





int main(int argc, char *argv[])

{
LinkedList list_instance;

cout << "1 LinkedList instance takes [" << sizeof(list_instance) << "] bytes in memory!"<< endl;

node node_instance;

cout << "1 node instance takes [" << sizeof(node_instance) <<"] bytes in memory !"<< endl;


    try{
    list = new LinkedList[index_size];
    }

     catch(std::bad_alloc) {
    cout << "Error allocating memory" << endl;
     return 0;
    //reboot code
    }

unsigned int some_data[] = {00, 01};
unsigned int index;

LinkedList *list_instance2 = NULL;



cout << "Allocating ..." << endl;

for (int i=0; i<allocations; i++)
{

index = rand();
unsigned short inde = (unsigned short)index;
list_instance2 = &list[inde];

list_instance2->insert(some_data, some_data[1]);


}

unsigned long sk = ((allocations * sizeof(node_instance) + index_size*sizeof(list_instance))) / (1024*1024*1024);

cout << "This process *should* consume around " << sk <<" GBytes of memory, but does it ?"<< endl;

cout << "Allocating done, *check the process size* and press any number key + ENTER to exit ..." << endl;

int u=0;
cin >> u;


return 0;
}
#包括
#包括
使用名称空间std;
结构节点
{
公众:
unsigned int part1;//4字节
无符号整数部分2;//4字节
node*next;//指针,64位系统上的8字节
无符号整数读取部分1();
};
结构链接列表
{
公众:
LinkedList();
无效插入(无符号整数数据[],无符号整数数据1);
bool isEmpty()常量;
节点*头;
};
无符号int节点::read_part1(){
返回部分1;
}
LinkedList::LinkedList():
头(空)
{
}
bool LinkedList::isEmpty()常量
{
返回值(head==NULL);
}
void LinkedList::insert(无符号整数数据[],无符号整数数据1)
{
节点*oldHead=head;
node*newHead=newnode();
newHead->part1=数据[0];
newHead->part2=data1;
newHead->next=oldHead;
head=新head;
}
无符号整数分配=300000000;
无符号整数索引大小=430000000//列表索引,430m,。
//将在堆上创建
LinkedList*list=NULL;
int main(int argc,char*argv[])
{
LinkedList\u实例;
cout在我的盒子上,带有(见下面的注释)

  • 它使用1243 MiB,而不是使用标准库堆例程的“预期”785 MiB
  • 当使用谷歌的tcmalloc时,它使用791 MiB
  • 当使用Boost对象池分配节点时,它使用840 MiB(使用标准库堆tcmalloc)
在堆例程的实现中,开销非常明显

代码如下:

  • 请注意此处使用的
    new(nothrow)
  • 另外,开始时的基线测量(我在linux上使用了
    pmap$(pgrep测试)| tail
  • 请注意插入中的选择:

    void LinkedList::insert(unsigned int data[], unsigned int data1)
    {
    #if 1
        head = new node { data[0], data1, head };
    #else
        static boost::object_pool<node> node_pool;
    
        node* add = node_pool.malloc();
        *add = node { data[0], data1, head };
        head = add;
    #endif
    }
    
    我把它改成了你可能想要的:

    list[rand() % index_size].insert(some_data, some_data[1]);
    

#包括
#包括
#包括
使用名称空间std;
结构节点
{
unsigned int part1;//4字节
无符号整数部分2;//4字节
node*next;//指针,64位系统上的8字节
};
结构链接列表
{
公众:
LinkedList();
无效插入(无符号整数数据[],无符号整数数据1);
bool isEmpty()常量;
节点*头;
};
LinkedList::LinkedList():
头(空)
{
}
bool LinkedList::isEmpty()常量
{
返回值(head==NULL);
}
void LinkedList::insert(无符号整数数据[],无符号整数数据1)
{
#如果1
head=新节点{data[0],data1,head};
#否则
静态boost::对象池节点池;
node*add=node_pool.malloc();
*add=节点{data[0],data1,head};
头=加;
#恩迪夫
}
常量无符号整数分配=30000000;
const unsigned int index_size=43000000;//列表索引
//将在堆上创建
LinkedList*list=NULL;
int main(int argc,char*argv[])
{
LinkedList\u实例;
cout在我的盒子上,带有(见下面的注释)

  • 它使用1243 MiB,而不是使用标准库堆例程的“预期”785 MiB
  • 当使用谷歌的tcmalloc时,它使用791 MiB
  • 当使用Boost对象池分配节点时,它使用840 MiB(使用标准库堆tcmalloc)
在堆例程的实现中,开销非常明显

代码如下:

  • 请注意此处使用的
    new(nothrow)
  • 另外,开始时的基线测量(我在linux上使用了
    pmap$(pgrep测试)| tail
  • 请注意插入中的选择:

    void LinkedList::insert(unsigned int data[], unsigned int data1)
    {
    #if 1
        head = new node { data[0], data1, head };
    #else
        static boost::object_pool<node> node_pool;
    
        node* add = node_pool.malloc();
        *add = node { data[0], data1, head };
        head = add;
    #endif
    }
    
    我把它改成了你可能想要的:

    list[rand() % index_size].insert(some_data, some_data[1]);
    

#包括
#包括
#包括
使用名称空间std;
结构节点
{
unsigned int part1;//4字节
无符号整数部分2;//4字节
node*next;//指针,64位系统上的8字节
};
结构链接列表
{
公众:
LinkedList();
无效插入(无符号整数数据[],无符号整数数据1);
bool isEmpty()常量;
节点*头;
};
LinkedList::LinkedList():
头(空)
{
}
bool LinkedList::isEmpty()常量
{
返回值(head==NULL);
}
void LinkedList::insert(无符号整数数据[],无符号整数数据1)
{
#如果1
head=新节点{data[0],data1,head};
#否则
静态boost::对象池节点池;
node*add=node_pool.malloc();
*add=节点{data[0],data1,head};
头=加;
#恩迪夫
}
常量无符号整数分配=30000000;
const unsigned int index_size=43000000;//列表索引
//将在堆上创建
LinkedList*list=NULL;
int main(int argc,char*argv[])
{
LinkedList\u实例;

您的代码只能进行1次分配:
list=newlinkedlist[index\u size]
,您在哪里/如何进行所有其他分配?当您分配430000000个LinkedList时,大约是3440000000字节。对不起,是的,我在原始帖子中忽略了分配。我编造了一些非常接近真实代码的方式。顺便说一句,LinkedList数组似乎分配正确(在使用被delete[]正确删除之后)。我将检查我的示例分配和报告是否仍然存在问题:)请不要忽略某些内容。当我们不知道您实际在做什么时,我们很难提供任何帮助。您的代码只执行1次分配:
list=new LinkedList[index\u size]
,您在哪里/如何进行所有其他分配?当您分配430000000个LinkedList时,大约是3440000000字节。对不起,是的,我在原始帖子中忽略了分配。我编造了一些非常接近真实代码的方式。顺便说一句,LinkedList数组似乎分配正确(在使用被删除[]正确删除后)。将