[{"data":1,"prerenderedAt":243},["ShallowReactive",2],{"tag-内存-1":3},{"title":4,"articles":5,"hot_article":28,"month_arch":91,"total":239,"cover":240},"内存 - 独孤影的博客",[6,18],{"id":7,"title":8,"uri":8,"author":9,"tags":10,"created_at":15,"view_count":16,"content":17},32,"关于阿里云ESC上go语言项目编译6l: running gcc failed: Cannot allocate memory","duguying",[11,12,13,14],"golang","编译","内存不够","swap","2015-10-28 14:06:02",4450,"\u003Cp>前段时间将自己的阿里云服务器上的系统由centos 6.5换为了ubuntu 14，其他的硬件配置都没有发生改变，将服务器上的数据恢复并且重新安装了golang的编译环境后，发现使用go build编译稍微大一点的golang项目就会报错：\u003C\u002Fp>\u003Cblockquote>\u003Cp>\u002Fusr\u002Flocal\u002Fgo\u002Fpkg\u002Ftool\u002Flinux_amd64\u002F6l: running gcc failed: Cannot allocate memory\u003C\u002Fp>\u003C\u002Fblockquote>\u003Cp>一直想不通为啥换了个系统就会报这个错，字面意思是gcc分配内存失败，应该是内存不够用，机器配置是1G内存，free -m 发现尚有400M的内存未使用，难道剩余400M的内存还不够go build命令编译代码使用？好吧，既然如此我就给它释放内存，kill掉众多的进程之后再进行go build编译，发现又可以编译了。之后发现偶尔能编译偶尔又不能编译，看样子确实是内存不够，可是为啥之前的centos系统上没有出现这种状况呢，一直不相信简单的“内存不够”就可以解释这一问题，因为之前的centos系统上是正常的，我甚至觉得可能是gcc版本的问题，猜测只有较高版本的gcc才会报这个错误。后来也曾在“golang天朝”论坛上发过帖子，并表达自己的猜测，认为不是内存不够这么简单，结果被别人鄙视不看英文……\u003C\u002Fp>\u003Cp>不想花钱升级机器硬件，难道我只有装回centos？今天执行free -m偶然间注意到了swap的数值貌似一直是空的，我思考若是我添加swap交换空间是否能解决这一问题呢，毕竟swap其实就是用硬盘空间虚拟出的内存，一个内存的缓冲区。于是就给它加了个1G的文件作为swap，居然直接就可以用go build，再也不用担心gcc对我说Cannot allocate memory了。简单的记录一下添加文件作为swap的步骤：\u003C\u002Fp>\u003Cul class=\" list-paddingleft-2\" style=\"list-style-type: disc;\">\u003Cli>\u003Cp>创建1个1GB的file\u003Cbr\u002F>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cpre class=\"brush:bash;toolbar:false\">sudo&nbsp;dd&nbsp;if=\u002Fdev\u002Fzero&nbsp;of=\u002Fmnt\u002F1GB.swap&nbsp;bs=1M&nbsp;count=1024\u003C\u002Fpre>\u003Cul class=\" list-paddingleft-2\" style=\"list-style-type: disc;\">\u003Cli>\u003Cp>格式化为Swap file\u003Cbr\u002F>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cpre class=\"brush:bash;toolbar:false\">sudo&nbsp;mkswap&nbsp;\u002Fmnt\u002F1GB.swap\u003C\u002Fpre>\u003Cul class=\" list-paddingleft-2\" style=\"list-style-type: disc;\">\u003Cli>\u003Cp>把swap file加入到系统中\u003Cbr\u002F>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cpre class=\"brush:bash;toolbar:false\">sudo&nbsp;swapon&nbsp;\u002Fmnt\u002F1GB.swap\u003C\u002Fpre>\u003Cul class=\" list-paddingleft-2\" style=\"list-style-type: disc;\">\u003Cli>\u003Cp>将swap永久添加\u003Cbr\u002F>在\u002Fect\u002Ffstab中加入新的Swap分区\u003Cbr\u002F>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cpre class=\"brush:bash;toolbar:false\">sudo&nbsp;gedit&nbsp;\u002Fetc\u002Ffstab\u003C\u002Fpre>\u003Cul class=\" list-paddingleft-2\" style=\"list-style-type: disc;\">\u003Cli>\u003Cp>在最后加入下列内容\u003Cbr\u002F>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cpre class=\"brush:bash;toolbar:false\">\u002Fmnt\u002F1GB.swap&nbsp;none&nbsp;swap&nbsp;sw&nbsp;0&nbsp;0\u003C\u002Fpre>\u003Cp>最后，free -m 命令可以看到swap的数据如下\u003C\u002Fp>\u003Cpre class=\"brush:bash;toolbar:false\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;used&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shared&nbsp;&nbsp;&nbsp;&nbsp;buffers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cached\nMem:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;992&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;903&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;57&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;188\n-\u002F+&nbsp;buffers\u002Fcache:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;656&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;335\nSwap:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1023&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1023\u003C\u002Fpre>\u003Cp>1G的内存交换区文件已经创建。\u003Cbr\u002F>\u003C\u002Fp>",{"id":19,"title":20,"uri":20,"author":9,"tags":21,"created_at":25,"view_count":26,"content":27},22,"内存越界的灾难",[22,23,24],"C语言","内存","越界","2014-12-06 19:06:10",3575,"\u003Cp>今天为了修复一个bug折腾了将近一天的时间，原本是计划在沙箱子项目里面添加命令行指定配置文件路径功能，结果被我发现之前留下的一个坑，但是整个发现过程花了一整下午的时间。\u003C\u002Fp>\u003Cp>调试过程中发现明明传入到函数中的路径printf是正确的，可是当执行到fopen时却直接异常退出，于是gdb跟踪调试\u003C\u002Fp>\u003Cp>\u003Cimg src=\"\u002F\u002Fduguying.oss-cn-qingdao.aliyuncs.com\u002F2014\u002F12\u002F6\u002Ffilename_interrupt.png\" title=\"\" alt=\"filename_interrupt.png\"\u002F>\u003C\u002Fp>\u003Cp>调试显示传入的文件名是正常的，但是当执行到#93步时文件名很明显被截断了，这是一件非常奇怪的事情，我甚至怀疑fopen函数是不是对传入文件名长度有限制，可事实上不是这样的。因为我后来专门写了个测试代码，同样的文件名路径，fopen却是正常的。我又怀疑是不是因为文件名字符串是malloc分配的原因，测试后却也是正常的。\u003Cbr\u002F>\u003C\u002Fp>\u003Cp>非常纠结，之后决定采用大段大段的砍掉无关代码的方法来更清晰的定位错误可能的位置，因为代码带多确实看着眼花缭乱。当我砍到只剩下调用config_read的时候，惊奇的发现上面内存初始化是这样的：\u003C\u002Fp>\u003Cpre class=\"brush:cpp;toolbar:false\">\u002F\u002F&nbsp;alloc&nbsp;memory&nbsp;for&nbsp;path&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;\nexecutable&nbsp;=&nbsp;(char*)malloc(sizeof(EXE_LEN));&nbsp;&nbsp;&nbsp;&nbsp;\nmemset(executable,&nbsp;0,&nbsp;sizeof(EXE_LEN));\u003C\u002Fpre>\u003Cp>shit! 这儿的EXE_LEN是int型指定文件名长度的啊，也就是说\u003Ccode>sizeof(EXE_LEN)\u003C\u002Fcode>是4byte，坑爹啊。但是我后面给这个内存strncpy了73个字符组成的字符串，嗯，编译器没给我报错，然后这内存几经传递传到了fopen里面，却被莫名的修改了，呵呵，修改了活该啊，谁让我把剩下的69个字符强行放入了它不该放的地方呢。其实应该是这个字符串越界到了fopen函数所使用的内存去，被修改也是太正常不过了。\u003C\u002Fp>\u003Cp>C语言编程确实需要小心谨慎，因为编译器给你的提示实在是太少，逻辑错了就会按照错的运行，结果便会是谬之千里。\u003C\u002Fp>\u003Cp>\u003Cbr\u002F>\u003C\u002Fp>",{"title":29,"list":30},"最热文章",[31,37,43,49,55,61,67,73,79,85],{"id":32,"title":33,"uri":34,"author":9,"created_at":35,"view_count":36},28,"docker容器目录与主机目录同步","\u002Farticle\u002Fdocker容器目录与主机目录同步","2014-12-31 12:34:08",15573,{"id":38,"title":39,"uri":40,"author":9,"created_at":41,"view_count":42},21,"beego中使用fis","\u002Farticle\u002Fbeego中使用fis","2014-12-03 10:47:56",9253,{"id":44,"title":45,"uri":46,"author":9,"created_at":47,"view_count":48},20,"用github登录，oauth开发","\u002Farticle\u002F用github登录，oauth开发","2014-11-23 13:33:37",9115,{"id":50,"title":51,"uri":52,"author":9,"created_at":53,"view_count":54},18,"windows服务注册工具nssm","\u002Farticle\u002Fwindows服务注册工具nssm","2014-10-31 21:34:39",9054,{"id":56,"title":57,"uri":58,"author":9,"created_at":59,"view_count":60},49,"用latex排版中文书籍","\u002Farticle\u002F用latex排版中文书籍","2016-10-31 11:12:02",8645,{"id":62,"title":63,"uri":64,"author":9,"created_at":65,"view_count":66},19,"texlive中文书签乱码","\u002Farticle\u002Ftexlive中文书签乱码","2014-11-15 02:54:01",8466,{"id":68,"title":69,"uri":70,"author":9,"created_at":71,"view_count":72},6,"Windows上的软件包管理器chocolatey","\u002Farticle\u002FWindows上的软件包管理器chocolatey","2014-08-10 15:55:22",8012,{"id":74,"title":75,"uri":76,"author":9,"created_at":77,"view_count":78},3,"利用开源和免费资源搭建CI环境","\u002Farticle\u002F利用开源和免费资源搭建CI环境","2014-07-04 16:54:58",7740,{"id":80,"title":81,"uri":82,"author":9,"created_at":83,"view_count":84},16,"php-fpm网站目录权限配置","\u002Farticle\u002Fphp-fpm网站目录权限配置","2014-10-18 01:36:10",7276,{"id":86,"title":87,"uri":88,"author":9,"created_at":89,"view_count":90},27,"sublime text 2插件开发，外部修改文件无法重新加载","\u002Farticle\u002Fsublime text 2插件开发，外部修改文件无法重新加载","2014-12-26 20:24:13",7055,{"title":92,"list":93},"按月归档",[94,98,101,104,107,110,113,117,121,124,127,130,134,137,140,144,147,150,153,157,160,163,166,169,172,175,178,181,184,187,190,193,196,199,202,205,208,211,214,217,221,224,227,230,233,236],{"title":95,"uri":96,"view_count":97},"2026年7月","\u002Farchive\u002F2026\u002F7\u002F1","[1]",{"title":99,"uri":100,"view_count":97},"2026年1月","\u002Farchive\u002F2026\u002F1\u002F1",{"title":102,"uri":103,"view_count":97},"2025年4月","\u002Farchive\u002F2025\u002F4\u002F1",{"title":105,"uri":106,"view_count":97},"2024年6月","\u002Farchive\u002F2024\u002F6\u002F1",{"title":108,"uri":109,"view_count":97},"2024年5月","\u002Farchive\u002F2024\u002F5\u002F1",{"title":111,"uri":112,"view_count":97},"2024年4月","\u002Farchive\u002F2024\u002F4\u002F1",{"title":114,"uri":115,"view_count":116},"2023年12月","\u002Farchive\u002F2023\u002F12\u002F1","[6]",{"title":118,"uri":119,"view_count":120},"2023年11月","\u002Farchive\u002F2023\u002F11\u002F1","[2]",{"title":122,"uri":123,"view_count":97},"2023年10月","\u002Farchive\u002F2023\u002F10\u002F1",{"title":125,"uri":126,"view_count":97},"2023年7月","\u002Farchive\u002F2023\u002F7\u002F1",{"title":128,"uri":129,"view_count":120},"2023年6月","\u002Farchive\u002F2023\u002F6\u002F1",{"title":131,"uri":132,"view_count":133},"2023年5月","\u002Farchive\u002F2023\u002F5\u002F1","[3]",{"title":135,"uri":136,"view_count":120},"2023年4月","\u002Farchive\u002F2023\u002F4\u002F1",{"title":138,"uri":139,"view_count":120},"2023年3月","\u002Farchive\u002F2023\u002F3\u002F1",{"title":141,"uri":142,"view_count":143},"2023年2月","\u002Farchive\u002F2023\u002F2\u002F1","[5]",{"title":145,"uri":146,"view_count":97},"2023年1月","\u002Farchive\u002F2023\u002F1\u002F1",{"title":148,"uri":149,"view_count":120},"2022年12月","\u002Farchive\u002F2022\u002F12\u002F1",{"title":151,"uri":152,"view_count":143},"2022年11月","\u002Farchive\u002F2022\u002F11\u002F1",{"title":154,"uri":155,"view_count":156},"2022年10月","\u002Farchive\u002F2022\u002F10\u002F1","[4]",{"title":158,"uri":159,"view_count":116},"2022年9月","\u002Farchive\u002F2022\u002F9\u002F1",{"title":161,"uri":162,"view_count":143},"2022年8月","\u002Farchive\u002F2022\u002F8\u002F1",{"title":164,"uri":165,"view_count":97},"2019年7月","\u002Farchive\u002F2019\u002F7\u002F1",{"title":167,"uri":168,"view_count":97},"2019年2月","\u002Farchive\u002F2019\u002F2\u002F1",{"title":170,"uri":171,"view_count":97},"2018年9月","\u002Farchive\u002F2018\u002F9\u002F1",{"title":173,"uri":174,"view_count":97},"2018年8月","\u002Farchive\u002F2018\u002F8\u002F1",{"title":176,"uri":177,"view_count":97},"2017年11月","\u002Farchive\u002F2017\u002F11\u002F1",{"title":179,"uri":180,"view_count":97},"2017年8月","\u002Farchive\u002F2017\u002F8\u002F1",{"title":182,"uri":183,"view_count":97},"2017年5月","\u002Farchive\u002F2017\u002F5\u002F1",{"title":185,"uri":186,"view_count":97},"2017年4月","\u002Farchive\u002F2017\u002F4\u002F1",{"title":188,"uri":189,"view_count":97},"2016年11月","\u002Farchive\u002F2016\u002F11\u002F1",{"title":191,"uri":192,"view_count":97},"2016年10月","\u002Farchive\u002F2016\u002F10\u002F1",{"title":194,"uri":195,"view_count":120},"2016年9月","\u002Farchive\u002F2016\u002F9\u002F1",{"title":197,"uri":198,"view_count":97},"2016年8月","\u002Farchive\u002F2016\u002F8\u002F1",{"title":200,"uri":201,"view_count":120},"2016年7月","\u002Farchive\u002F2016\u002F7\u002F1",{"title":203,"uri":204,"view_count":143},"2016年6月","\u002Farchive\u002F2016\u002F6\u002F1",{"title":206,"uri":207,"view_count":97},"2015年12月","\u002Farchive\u002F2015\u002F12\u002F1",{"title":209,"uri":210,"view_count":97},"2015年10月","\u002Farchive\u002F2015\u002F10\u002F1",{"title":212,"uri":213,"view_count":120},"2015年2月","\u002Farchive\u002F2015\u002F2\u002F1",{"title":215,"uri":216,"view_count":97},"2015年1月","\u002Farchive\u002F2015\u002F1\u002F1",{"title":218,"uri":219,"view_count":220},"2014年12月","\u002Farchive\u002F2014\u002F12\u002F1","[8]",{"title":222,"uri":223,"view_count":120},"2014年11月","\u002Farchive\u002F2014\u002F11\u002F1",{"title":225,"uri":226,"view_count":143},"2014年10月","\u002Farchive\u002F2014\u002F10\u002F1",{"title":228,"uri":229,"view_count":133},"2014年9月","\u002Farchive\u002F2014\u002F9\u002F1",{"title":231,"uri":232,"view_count":133},"2014年8月","\u002Farchive\u002F2014\u002F8\u002F1",{"title":234,"uri":235,"view_count":133},"2014年7月","\u002Farchive\u002F2014\u002F7\u002F1",{"title":237,"uri":238,"view_count":120},"2013年9月","\u002Farchive\u002F2013\u002F9\u002F1",2,{"url":241,"title":242},"https:\u002F\u002Fimage.duguying.net\u002F2022\u002F09\u002Fij6x8o4s.webp","老家 摄于2020年秋",1790781595827]