怎么解决 LINUX 堆栈溢出内存的问题

怎么解决 LINUX 堆栈溢出内存的问题,第1张

一般是由于数组指针访问越界

或者是野指针造成

解决的办法:可以将代码按功能段,一段一段测试,

//测试一块代码时,先注释掉其它代码

找出错误出现的地方,修正

如果,因为程序里使用的堆栈大小大于默认的造成出错

需要修改默认的堆栈大小的值

一、堆内存相关配置

设置堆初始值

指令1:-Xms2g

指令2:-XX:InitialHeapSize=2048m

设置堆区最大值

指令1:`-Xmx2g`

指令2: -XX:MaxHeapSize=2048m

缩小堆内存的时机

-XX:MaxHeapFreeRatio=70//堆内存使用率大于70时扩张堆内存,xms=xmx时该参数无效,默认值70

扩张堆内存的时机

-XX:MinHeapFreeRatio=40//堆内存使用率小于40时缩减堆内存,xms=xmx时该参数无效,默认值40

新生代内存配置

指令1:-Xmn512m

指令2:-XX:MaxNewSize=512m

2个survivor区和Eden区大小比率

指令:-XX:SurvivorRatio=6 //S区和Eden区占新生代比率为1:6,两个S区2:6

新生代和老年代的占比

-XX:NewRatio=4 //表示新生代:老年代 = 1:4 即老年代占整个堆的4/5;默认值=2

二、方法区内存配置常用参数

初始化的Metaspace大小,

-XX:MetaspaceSize :

Metaspace最大值

-XX:MaxMetaspaceSize

三、线程栈内存配置常用参数

每个线程栈最大值

指令1:-Xss256k

指令2:-XX:ThreadStackSize=256k

注意:

栈设置太大,会导致线程创建减少。

栈设置小,会导致深入不够,深度的递归会导致栈溢出。

建议栈深度设置在3000-5000

四、配置垃圾收集器

Serial垃圾收集器(新生代)

开启:-XX:+UseSerialGC

关闭:-XX:-UseSerialGC

//新生代使用Serial 老年代则使用SerialOld

ParNew垃圾收集器(新生代)

开启 -XX:+UseParNewGC

关闭 -XX:-UseParNewGC

//新生代使用功能ParNew 老年代则使用功能CMS

Parallel Scavenge收集器(新生代)

开启 -XX:+UseParallelOldGC

关闭 -XX:-UseParallelOldGC

//新生代使用功能Parallel Scavenge 老年代将会使用Parallel Old收集器

ParallelOl垃圾收集器(老年代)

开启 -XX:+UseParallelGC

关闭 -XX:-UseParallelGC

//新生代使用功能Parallel Scavenge 老年代将会使用Parallel Old收集器

CMS垃圾收集器(老年代)

开启 -XX:+UseConcMarkSweepGC

关闭 -XX:-UseConcMarkSweepGC

G1垃圾收集器

开启 -XX:+UseG1GC

关闭 -XX:-UseG1GC

五、GC策略配置

GC并行执行线程数

-XX:ParallelGCThreads=16

新生代可容纳的最大对象

-XX:PretenureSizeThreshold=1000000 //大于此值的对象直接会分配到老年代,设置为0则没有限制。 //避免在Eden区和Survivor区发生大量的内存复制,该参数只对Serial和ParNew收集器有效,Parallel Scavenge并不认识该参数

进入老年代的GC年龄

进入老年代最小的GC年龄

-XX:InitialTenuringThreshol=7 //年轻代对象转换为老年代对象最小年龄值,默认值7,对象在坚持过一次Minor GC之后,年龄就加1,每个对象在坚持过一次Minor GC之后,年龄就增加1

进入老年代最大的GC年龄

-XX:MaxTenuringThreshold=15 //年轻代对象转换为老年代对象最大年龄值,默认值15

六、GC日志信息配置

配置GC文件路径

-Xloggc:/data/gclog/gc.log//固定路径名称生成 -Xloggc:/home/GCEASY/gc-%t.log //根据时间生成

滚动生成日志

日志文件达到一定大小后,生成另一个文件。须配置Xloggc

开启 -XX:+UseGCLogFileRotation

关闭 -XX:-UseGCLogFileRotation

-XX:NumberOfGCLogFiles=4 //滚动GC日志文件数,默认0,不滚动 -XX:GCLogFileSize=100k //GC文件滚动大小,需配置UseGCLogFileRotation,设置为0表示仅通过jcmd命令触发

Valgrind 是一款 Linux下(支持 x86、x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的 malloc 和 free,以及 C++ 中的 new 和 delete),找出内存泄漏问题。

Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误:

使用未初始化的内存 (Use of uninitialised memory)

使用已经释放了的内存 (Reading/writing memory after it has been free’d)

使用超过 malloc 分配的内存空间(Reading/writing off the end of malloc’d blocks)

对堆栈的非法访问(Reading/writing inappropriate areas on the stack)

申请的空间是否有释放(Memory leaks – where pointers to malloc’d blocks are lost forever)

malloc/free/new/delete 申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])

src 和 dst 的重叠(Overlapping src and dst pointers in memcpy() and related functions)

重复 free

① 编译安装 Valgrind:

# wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2

# tar xvf valgrind-3.4.1.tar.bz2

# cd valgrind-3.4.1/

# ./configure

…………

Primary build target: X86_LINUX

Secondary build target:

Default

supp files: exp-ptrcheck.supp xfree-3.supp xfree-4.supp

glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.5.supp

# make

# make install

# whereis valgrind

valgrind:

/usr/bin/valgrind

/usr/lib/valgrind

/usr/local/bin/valgrind

/usr/local/lib/valgrind

/usr/include/valgrind

/usr/share/man/man1/valgrind.1.gz

运行程序

使用示例:对“ls”程序进程检查,返回结果中的“definitely lost: 0 bytes in 0 blocks.”表示没有内存泄漏。

# /usr/local/bin/valgrind --tool=memcheck --leak-check=full ls /

==29801== Memcheck, a memory error detector.

==29801== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.

==29801== Using LibVEX rev 1884, a library for dynamic binary translation.

==29801== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.

==29801== Using valgrind-3.4.1, a dynamic binary instrumentation framework.

==29801== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.

==29801== For more details, rerun with: -v

==29801==

bin etc lost+found mnt proc selinuxsys usr

boot home media net root smokeping tftpboot var

dev lib miscopt sbin srvtmp

==29801==

==29801== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 1)

==29801== malloc/free: in use at exit: 14,744 bytes in 32 blocks.

==29801== malloc/free: 162 allocs, 130 frees, 33,758 bytes allocated.

==29801== For counts of detected errors, rerun with: -v

==29801== searching for pointers to 32 not-freed blocks.

==29801== checked 139,012 bytes.

==29801==

==29801== LEAK SUMMARY:

==29801==definitely lost: 0 bytes in 0 blocks.

==29801== possibly lost: 0 bytes in 0 blocks.

==29801==still reachable: 14,744 bytes in 32 blocks.

==29801== suppressed: 0 bytes in 0 blocks.

==29801== Reachable blocks (those to which a pointer was found) are not shown.

==29801== To see them, rerun with: --leak-check=full --show-reachable=yes

---------------------------------------------------------------------------------------------------------------

# /usr/local/bin/valgrind --tool=memcheck --leak-check=full ps /

==29898== Memcheck, a memory error detector.

==29898== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.

==29898== Using LibVEX rev 1884, a library for dynamic binary translation.

==29898== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.

==29898== Using valgrind-3.4.1, a dynamic binary instrumentation framework.

==29898== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.

==29898== For more details, rerun with: -v

==29898==

ERROR: Garbage option.

********* simple selection ********* ********* selection by list *********

-A all processes -C by command name

-N negate selection -G by real group ID (supports names)

-a all w/ tty except session leaders -U by real user ID (supports names)

-d all except session leaders -g by session OR by effective group name

-e all processes -p by process ID

T all processes on this terminal -s processes in the sessions given

a all w/ tty, including other users -t by tty

g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)

r only running processes U processes for specified users

x processes w/o controlling ttys t by tty

*********** output format ********** *********** long options ***********

-o,o user-defined -f full--Group --User --pid --cols --ppid

-j,j job control s signal --group --user --sid --rows --info

-O,O preloaded -o v virtual memory --cumulative --format --deselect

-l,l long u user-oriented --sort --tty --forest --version

-F extra fullX registers --heading --no-heading --context

********* misc options *********

-V,V show version L list format codes f ASCII art forest

-m,m,-L,-T,H threads S children in sum-y change -l format

-M,Z security data c true command name -c scheduling class

-w,w wide output n numeric WCHAN,UID -H process hierarchy

==29898==

==29898== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 14 from 1)

==29898== malloc/free: in use at exit: 16 bytes in 2 blocks.

==29898== malloc/free: 20 allocs, 18 frees, 2,344 bytes allocated.

==29898== For counts of detected errors, rerun with: -v

==29898== searching for pointers to 2 not-freed blocks.

==29898== checked 263,972 bytes.

==29898==

==29898== 8 bytes in 1 blocks are definitely lost in loss record 2 of 2

==29898==at 0x4005A88: malloc (vg_replace_malloc.c:207)

==29898==by 0xBFF6DF: strdup (in /lib/libc-2.5.so)

==29898==by 0x804A464: (within /bin/ps)

==29898==by 0x804A802: (within /bin/ps)

==29898==by 0x804964D: (within /bin/ps)

==29898==by 0xBA5E8B: (below main) (in /lib/libc-2.5.so)

==29898==

==29898== LEAK SUMMARY:

==29898==definitely lost: 8 bytes in 1 blocks.

==29898== possibly lost: 0 bytes in 0 blocks.

==29898==still reachable: 8 bytes in 1 blocks.

==29898== suppressed: 0 bytes in 0 blocks.

==29898== Reachable blocks (those to which a pointer was found) are not shown.

==29898== To see them, rerun with: --leak-check=full --show-reachable=yes


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7482997.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存