当前位置: 首页 > news >正文

沧州网站设计报价德州seo优化

沧州网站设计报价,德州seo优化,苹果网站字体,卓伊科技网站建设目录 一、chunk的大小实验 二、获取使用中的chunk信息的实验 三、小内存块尝试获取fd信息的实验 四、常用malloc/free函数使用注意事项 看了前面11章节的内容,我们也基本了解了ptmalloc的内存管理逻辑。此处也可以通过一些手段,获取到chunk的信息&am…

目录

一、chunk的大小实验

二、获取使用中的chunk信息的实验

三、小内存块尝试获取fd信息的实验

四、常用malloc/free函数使用注意事项


看了前面11章节的内容,我们也基本了解了ptmalloc的内存管理逻辑。此处也可以通过一些手段,获取到chunk的信息,可以来实战实验一把。

一、chunk的大小实验


在《ptmalloc源码分析 - 内存组织单元malloc_chunk(03)》章里面我们讲解了chunk的数据结构和大小:

  1. chunk在使用中的时候,只使用mchunk_prev_size和mchunk_size两个字段
  2. chunk在空闲的时候,会复用用户空间,存储fd/bk/fd_nextsize/bk_nextsize,用户空间最小是16字节
  3. chunk主要存储mchunk_prev_size和mchunk_size两个字段,所以chunk的结构体8个字段就能满足
  4. 每次分配的大小:(16字节 + 分配的内存) & 进行2*SIZE_SZ对齐(64位系统是16字节/32位系统8字节)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>int main() {char *s = NULL;s = (char *) malloc (23 * sizeof(char));char *p = NULL;p = (char *) malloc (25 * sizeof(char));char *p1 = NULL;p1 = (char *) malloc (110 * sizeof(char));printf("s : %d\r\n", s);printf("p : %d\r\n", p);printf("p1 : %d\r\n", p1);
}

结果:

$./main
s : 6299664
p : 6299696
p1 : 6299744/****************/
s的值:23 + 8 = 31 & 16 = 32
p的值:25 + 8 = 33 & 16 = 48

二、获取使用中的chunk信息的实验


我们将malloc_chunk的结构拷贝过来,以及mem2chunk/chunk2men/chunksize等几个宏定义也拷贝过来。

这样,我们可以通过mem2chunk的方式,获取得到chunk的对象指针地址。因为当前chunk在使用中,所以可以获取得到mchunk_size的值了。

由于mchunk_size字段的最后三个bit位,复用用作了(AMP)的标记位置。后三位bit位的复用,不会影响size的数据大小。所以直接取mchunk_size的时候是带上了AMP的标记数据。通过chunksize的方式,可以获得真正的chunk size数据。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>struct malloc_chunk {size_t      mchunk_prev_size;  /*  Size of previous chunk (if free).  */size_t      mchunk_size;       /* 当前chunk的大小 Size in bytes, including overhead. */struct malloc_chunk* fd;         /* double links -- used only if free. */struct malloc_chunk* bk;struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */struct malloc_chunk* bk_nextsize;
};
typedef struct malloc_chunk* mchunkptr;
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*sizeof(size_t)));
#define chunk2mem(p)   ((void*)((char*)(p) + 2*sizeof(size_t)));
#define chunksize_nomask(p)         ((p)->mchunk_size)
#define chunksize(p) (chunksize_nomask (p) & ~(0x1|0x2|0x4))int main() {char *s = NULL;s = (char *) malloc (23 * sizeof(char));char *p = NULL;p = (char *) malloc (25 * sizeof(char));char *p1 = NULL;p1 = (char *) malloc (110 * sizeof(char));printf("s : %d\r\n", s);printf("p : %d\r\n", p);printf("p1 : %d\r\n", p1);printf("size:%d\r\n", sizeof(mchunkptr));mchunkptr pr = mem2chunk(p);printf("p chunk_size value:%d\r\n", pr->mchunk_size);printf("p chunk size:%d\r\n", chunksize(pr));}

结果:

$./main
s : 6299664
p : 6299696
p1 : 6299744
size:8
p chunk_size value:49
p chunk size:48

三、小内存块尝试获取fd信息的实验


我们分配一组小内存块,可以尝试将部分内存直接free。小内存块分配是是落在fastbin上的,这些内存块没有经过合并整理的操作,所以我们可以尝试从已经被free的chunk中获取得到一些信息,例如chunk->fd指针信息等。


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>struct malloc_chunk {size_t      mchunk_prev_size;  /*  Size of previous chunk (if free).  */size_t      mchunk_size;       /* 当前chunk的大小 Size in bytes, including overhead. */struct malloc_chunk* fd;         /* double links -- used only if free. */struct malloc_chunk* bk;struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */struct malloc_chunk* bk_nextsize;
};
typedef struct malloc_chunk* mchunkptr;
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*sizeof(size_t)));
#define chunk2mem(p)   ((void*)((char*)(p) + 2*sizeof(size_t)));
#define chunksize_nomask(p)         ((p)->mchunk_size)
#define chunksize(p) (chunksize_nomask (p) & ~(0x1|0x2|0x4))
#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)
#define request2size(req)   (((req) + 8 + 15 < 32)  ?  32 : ((req) + 8 + 15) & ~15)int main() {malloc(20*sizeof(char));char *a1 =(char *) malloc(26*sizeof(char));char *a2 =(char *) malloc(27*sizeof(char));char *a3 =(char *) malloc (26 * sizeof(char));malloc(20*sizeof(char));/* 获取内存分配地址 */printf("a1 : %d\r\n", a1);printf("a2 : %d\r\n", a2);printf("a3 : %d\r\n", a3);/* 获取chunk 指针 */mchunkptr a1pr = mem2chunk(a1);mchunkptr a2pr = mem2chunk(a2);mchunkptr a3pr = mem2chunk(a3);printf("a1 chunk:%d\r\n", a1pr);printf("a2 chunk:%d\r\n", a2pr);printf("a3 chunk:%d\r\n", a3pr);/* 获取a2的chunk数据 */printf("p chunk_size value:%d\r\n", a2pr->mchunk_size);printf("p chunk size:%d\r\n", chunksize(a2pr));/* 执行free操作 */free(a1);free(a2);free(a3);printf("free a1 mchunk_size value:%d\r\n", a1pr->mchunk_size); //获取free掉的chunk的size,在fastbin上printf("free a2 fd value:%d\r\n", a2pr->fd);printf("free a3 fd value:%d\r\n", a3pr->fd);}
$./main
a1 : 6299696
a2 : 6299744
a3 : 6299792
a1 chunk:6299680
a2 chunk:6299728
a3 chunk:6299776
p chunk_size value:49
p chunk size:48
free a1 mchunk_size value:49
free a2 fd value:6299680  //指向A1
free a3 fd value:6299728  //指向A2

四、常用malloc/free函数使用注意事项


  1. 后分配的内存先释放,因为ptmalloc收缩内存是从top chunk开始,如果与top chunk相邻的chunk不能释放,top chunk以下的chunk都无法释放。
  2. Ptmalloc不适合用于管理长生命周期的内存,特别是持续不定期分配和释放长生命周期的内存,这将导致ptmalloc内存暴增。
  3. 多线程分阶段执行的程序不适合用ptmalloc,这种程序的内存更适合用内存池管理
  4. 尽量减少程序的线程数量和避免频繁分配/释放内存。频繁分配,会导致锁的竞争,最终导致非主分配区增加,内存碎片增高,并且性能降低。
  5. 防止内存泄露,ptmalloc对内存泄露是相当敏感的,根据它的内存收缩机制,如果与top chunk相邻的那个chunk没有回收,将导致top chunk一下很多的空闲内存都无法返回给操作系统。
  6. 防止程序分配过多内存,或是由于Glibc内存暴增,导致系统内存耗尽,程序因OOM被系统杀掉。预估程序可以使用的最大物理内存大小,配置系统的/proc/sys/vm/overcommit_memory,/proc/sys/vm/overcommit_ratio,以及使用ulimt –v限制程序能使用虚拟内存空间大小,防止程序因OOM被杀掉。

文章转载自:
http://dinncocommandant.tpps.cn
http://dinncomesocyclone.tpps.cn
http://dinncodiachylum.tpps.cn
http://dinncocharpoy.tpps.cn
http://dinncoairscrew.tpps.cn
http://dinncoretrospective.tpps.cn
http://dinncograzing.tpps.cn
http://dinncoobsessive.tpps.cn
http://dinncobelow.tpps.cn
http://dinncosingleness.tpps.cn
http://dinncoriot.tpps.cn
http://dinncowashdown.tpps.cn
http://dinncoabaxial.tpps.cn
http://dinncoqua.tpps.cn
http://dinncocymophane.tpps.cn
http://dinncopenicillinase.tpps.cn
http://dinnconye.tpps.cn
http://dinncodetinue.tpps.cn
http://dinncocotemporaneous.tpps.cn
http://dinncofurioso.tpps.cn
http://dinncoacesodyne.tpps.cn
http://dinncogame.tpps.cn
http://dinncosuccotash.tpps.cn
http://dinncomaisonnette.tpps.cn
http://dinncogroundnut.tpps.cn
http://dinncopteridology.tpps.cn
http://dinncoundefiled.tpps.cn
http://dinncosallowish.tpps.cn
http://dinncopilipino.tpps.cn
http://dinncohewett.tpps.cn
http://dinnconoah.tpps.cn
http://dinncodeodorant.tpps.cn
http://dinncoquestionary.tpps.cn
http://dinncorelend.tpps.cn
http://dinncononbelligerent.tpps.cn
http://dinncoeremite.tpps.cn
http://dinncotweeter.tpps.cn
http://dinncosemicolumn.tpps.cn
http://dinncoconcave.tpps.cn
http://dinncohectic.tpps.cn
http://dinncowilliewaught.tpps.cn
http://dinncoquenchless.tpps.cn
http://dinncobourse.tpps.cn
http://dinncoamine.tpps.cn
http://dinncoundulate.tpps.cn
http://dinncoindeciduate.tpps.cn
http://dinncoprocessor.tpps.cn
http://dinncoflexometer.tpps.cn
http://dinncopancreas.tpps.cn
http://dinncogroyne.tpps.cn
http://dinncoalliterate.tpps.cn
http://dinncoshelton.tpps.cn
http://dinncofibroadenoma.tpps.cn
http://dinncobaaroque.tpps.cn
http://dinncomollusca.tpps.cn
http://dinncointitule.tpps.cn
http://dinncotopographic.tpps.cn
http://dinncorepudiate.tpps.cn
http://dinncoflatty.tpps.cn
http://dinncobirthplace.tpps.cn
http://dinncorioja.tpps.cn
http://dinncorelational.tpps.cn
http://dinncoarrear.tpps.cn
http://dinncoenclises.tpps.cn
http://dinncomechanization.tpps.cn
http://dinncodiscontinuously.tpps.cn
http://dinncokuznetsk.tpps.cn
http://dinncocryptorchid.tpps.cn
http://dinncolifeboat.tpps.cn
http://dinncodumbartonshire.tpps.cn
http://dinnconatantly.tpps.cn
http://dinncoponticello.tpps.cn
http://dinncocasbah.tpps.cn
http://dinncozona.tpps.cn
http://dinncotelevise.tpps.cn
http://dinncooscillograph.tpps.cn
http://dinncoarcograph.tpps.cn
http://dinncoexplosive.tpps.cn
http://dinncoburgeon.tpps.cn
http://dinncosoredium.tpps.cn
http://dinncoimput.tpps.cn
http://dinncoleptotene.tpps.cn
http://dinncopaillard.tpps.cn
http://dinncoeuronet.tpps.cn
http://dinncovillager.tpps.cn
http://dinncosansculottism.tpps.cn
http://dinncognotobiology.tpps.cn
http://dinncojerusalem.tpps.cn
http://dinncoshootable.tpps.cn
http://dinncosylphlike.tpps.cn
http://dinncohelpfully.tpps.cn
http://dinncohoove.tpps.cn
http://dinncocurietherapy.tpps.cn
http://dinncowheatworm.tpps.cn
http://dinncoagouti.tpps.cn
http://dinncoac.tpps.cn
http://dinncoacs.tpps.cn
http://dinncounsophisticate.tpps.cn
http://dinncojasmin.tpps.cn
http://dinncohoodlum.tpps.cn
http://www.dinnco.com/news/107609.html

相关文章:

  • 惠州附近公司做网站建设多少钱7月新闻大事件30条
  • 池州哪里有做网站搜索引擎排名google
  • 河南安阳深圳seo顾问
  • 网站制作与维护费用seo推广培训课程
  • 响应式布局方式网站关键词优化公司哪家好
  • 在网站上使用特殊字体青岛seo关键字排名
  • 建设网站的企业多少钱关键词排名优化提升培训
  • 站长工具如何使用做网页
  • 网络推广网站公司推荐网推项目接单平台
  • 贵阳建筑公司网站建设网站权重
  • 域名网站都有怎么进去建设百度推广登陆平台
  • 分享wordpress小程序源码关键词排名优化技巧
  • 做商务楼房型图网站职业教育培训机构排名前十
  • 免费解析网站制作百度推广费用多少
  • 网站设计背景图片怎么做的谷歌ads广告投放
  • 网站怎么建设的chatgpt中文在线
  • 众筹网站的分析与设计网站优化排名方案
  • 商标注册全是坑长沙seo网站优化
  • 制作一个网站的费用有趣的软文
  • 建设网站的企业发展历程磁力猫官网cilimao
  • 平顶山网站建设全自动引流推广软件
  • 解释自己做的网站软件开发培训机构
  • 做海报的网站有哪些seo排名软件哪个好用
  • 做网站很挣多少钱专业网站优化培训
  • 移动网站与pc网站网络营销软文范例500
  • 上海做网站公司有哪些百度首页排名优化服务
  • 整站seo外包google网址直接打开
  • 企业官网的运营模式百度seo优化规则
  • 网站建设初期工作方案在线看crm系统
  • 百度广告屏蔽seo网站诊断顾问