DEB9与DEB10中的内存消耗
在以下C ++程序中,我试图通过将指针逐一1页将较大的内存块分为较小的子盖。在创建子块时,我正在初始化64位结构的变量。我试图在Debian10和Debian9上运行此代码。我观察到的是在DEB9中仅消耗64位存储器,但DEB10消耗了4K的整个页面。 有人可以帮助我理解是什么原因导致这种差异?这可以与内核处理内存的方式有关吗?感谢您提前的帮助。
#include <vector>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;
struct e{
u8* f;
u64* s;
};
struct se{
u64 cid;
void* np;
};
const u64 a = -1;
u32 subchunk = 2878;
u32 subchunkperchunk = 140;
u32 chunksize = 1028096; //in bytes
u32 TSize = 4096;
u32 eleOfChunk = 50;
u32 eSize = 4096;
u64 x;
vector<e> v;
int it=0;
u64& add(const void* ep)
{
void* subChunkPtr = (void*) (((u64)ep) / chunksize * chunksize);
u64 firstElementLocation = ((u64)subChunkPtr) + TSize;
u32 indexInsideSubChunk = (((u64)ep)-firstElementLocation) / eSize;
se* elePtr = ((se*)subChunkPtr) + indexInsideSubChunk;
return elePtr->cid;
}
int main()
{
int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU);
dup2(file, 1);
for (u32 i=0; i<= subchunk/subchunkperchunk; i++)
{
u32 eleInCurrChunk;
if (i != subchunk/subchunkperchunk)
{
eleInCurrChunk = subchunkperchunk;
}
else
{
eleInCurrChunk = subchunk % subchunkperchunk;
if (eleInCurrChunk == 0)
{
break;
}
}
u32 CSize = (eleInCurrChunk+1) * chunksize;
u8* sp;
sp = new u8[CSize];
u8* ssp = sp + (chunksize - ((u64)sp % chunksize));
for (u32 j = 0; j < eleInCurrChunk; ++j) //creat block
{
u8* scp = ssp + j * chunksize;
u8* ep = scp + TSize;
for (u32 k=0; k < eleOfChunk ; ++k) //creat subblock
{
v.push_back(e());
v[it].f = ep;
v[it].s = &add(ep);
add(ep)= a; //set the 64 bits to high
ep += eSize;
++it;
}
system("free -m");
}
}
return 0;
}
Below is the output of the code on deb10
total used free shared buff/cache available
Mem: 7914 1789 4306 0 1819 5826
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1789 4306 0 1819 5826
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1791 4304 0 1819 5824
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1791 4304 0 1819 5824
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1793 4302 0 1819 5822
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1793 4302 0 1819 5822
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1795 4300 0 1819 5820
Swap: 0 0 0
.
.
.
Mem: 7914 4592 1502 0 1819 3022
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4594 1500 0 1819 3020
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4594 1501 0 1819 3021
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4596 1499 0 1819 3019
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4596 1499 0 1819 3019
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4598 1497 0 1819 3017
Swap: 0 0 0
If you check the memory usage on "used" it is consuming ~4.5G by the end of the iteration.
Below is the output of the code on deb9.
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
.
.
.
total used free shared buff/cache available
Mem: 7923 1870 4101 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4101 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4100 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4100 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4101 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4100 4 1952 5750
Swap: 0 0 0
到迭代结束时,它消耗了〜1.8克 从输出中可以看到,DEB9和DEB10之间存在巨大的内存消耗差异。当相同的代码在不同的debian机器上运行时,我想知道为什么这是不同的(即deb9和deb10)。
In the below c++ program I am trying to divide a bigger memory block into smaller subblock by moving pointers by 1 page. While creating a subblock I am initializing the variable of a structure which is of 64 bits. I tried to run this code on debian10 and debian9. What I observed is in deb9 only 64 bits memory is consumed but deb10 consumes the entire page which is off 4K.
Can someone help me understand what causes this difference? Can this be related to the way the kernel handles the memory? Thanks for your help in advance.
#include <vector>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;
struct e{
u8* f;
u64* s;
};
struct se{
u64 cid;
void* np;
};
const u64 a = -1;
u32 subchunk = 2878;
u32 subchunkperchunk = 140;
u32 chunksize = 1028096; //in bytes
u32 TSize = 4096;
u32 eleOfChunk = 50;
u32 eSize = 4096;
u64 x;
vector<e> v;
int it=0;
u64& add(const void* ep)
{
void* subChunkPtr = (void*) (((u64)ep) / chunksize * chunksize);
u64 firstElementLocation = ((u64)subChunkPtr) + TSize;
u32 indexInsideSubChunk = (((u64)ep)-firstElementLocation) / eSize;
se* elePtr = ((se*)subChunkPtr) + indexInsideSubChunk;
return elePtr->cid;
}
int main()
{
int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU);
dup2(file, 1);
for (u32 i=0; i<= subchunk/subchunkperchunk; i++)
{
u32 eleInCurrChunk;
if (i != subchunk/subchunkperchunk)
{
eleInCurrChunk = subchunkperchunk;
}
else
{
eleInCurrChunk = subchunk % subchunkperchunk;
if (eleInCurrChunk == 0)
{
break;
}
}
u32 CSize = (eleInCurrChunk+1) * chunksize;
u8* sp;
sp = new u8[CSize];
u8* ssp = sp + (chunksize - ((u64)sp % chunksize));
for (u32 j = 0; j < eleInCurrChunk; ++j) //creat block
{
u8* scp = ssp + j * chunksize;
u8* ep = scp + TSize;
for (u32 k=0; k < eleOfChunk ; ++k) //creat subblock
{
v.push_back(e());
v[it].f = ep;
v[it].s = &add(ep);
add(ep)= a; //set the 64 bits to high
ep += eSize;
++it;
}
system("free -m");
}
}
return 0;
}
Below is the output of the code on deb10
total used free shared buff/cache available
Mem: 7914 1789 4306 0 1819 5826
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1789 4306 0 1819 5826
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1791 4304 0 1819 5824
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1791 4304 0 1819 5824
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1793 4302 0 1819 5822
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1793 4302 0 1819 5822
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 1795 4300 0 1819 5820
Swap: 0 0 0
.
.
.
Mem: 7914 4592 1502 0 1819 3022
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4594 1500 0 1819 3020
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4594 1501 0 1819 3021
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4596 1499 0 1819 3019
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4596 1499 0 1819 3019
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7914 4598 1497 0 1819 3017
Swap: 0 0 0
If you check the memory usage on "used" it is consuming ~4.5G by the end of the iteration.
Below is the output of the code on deb9.
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1843 4128 4 1951 5776
Swap: 0 0 0
.
.
.
total used free shared buff/cache available
Mem: 7923 1870 4101 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4101 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4100 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4100 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4101 4 1952 5750
Swap: 0 0 0
total used free shared buff/cache available
Mem: 7923 1870 4100 4 1952 5750
Swap: 0 0 0
By the end of the iteration it is consuming ~1.8G
As we see from the output there is a huge memory consumption difference between deb9 and deb10. I would like to know why this is different, when the same code runs on different Debian machines (i.e. deb9 and deb10).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论