Hi ,
This issue can be replicated by executing the above mentioned code on
debian9 and debian10 OS. The compiler arguments I used is g++ debian.cc
(debian.cc is the file name). After compiling we need to check the output
file(i.e Result). In the output file focus on the 'used' field of "free -m"
cmd. You can see the memory usage difference between debian9 and debian10.
To measure memory usage I use the "free -m" command and I focused on the
"used" field to check how much memory has been consumed.
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).

On Fri, 1 Jul 2022 at 18:27, Anu Anu <[email protected]> wrote:

> Hi,
> 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;
> }
>
>

Reply via email to