zoukankan      html  css  js  c++  java
  • LINUX下C-C++类软件的诊断

    一般C/C++写程序由于内存、指针等问题在后期的过程都一般会遇到内存等资源泄露、崩溃等问题,对于这些问题,一般从哪个角度解决呢?

    下面从几个角度进行总结:

    内存泄露

    首先需要注意一个问题是top或者htop显示的内存使用中cache的问题,这个和windows中的任务管理器看到的有差别。

    具体分析如下:

    在linux读写文件时,它用于缓存物理磁盘上的磁盘块,从而加快对磁盘上数据的访问。

    buffer cache的内容对应磁盘上一个块(block),块通常为1K,都是连续的。在linux下,为了更有效的使用物理内存,操作系统自动使用所有空闲内存作为Buffer Cache使用。当程序需要更多内存时,操作系统会自动减小Cache的大小。我们在观察Linux的内存使用情况时,只要没发现用swap的交换空间,就不必担心自己的内存太少。http://blog.csdn.net/heizistudio/article/details/25125061

    如下图,htop显示的数据,其中长时运行时,Mem项有时会使用完了,此时并不表明程序泄露占用,检查真正的进程项目的RES项的内存使用,这个是正在使用的,或者执行 pmap -d 某个进程号,也可以查看

    wps100.tmp

    [htop还可以直观的查看线程情况]

    有了这些问题,那我们有更好的办法避免吗?答案是:

    Ø 智能指针

    n C++ 11标准已经有了,stl中的auto_ptr尽量还是避免吧

    n boost的share_ptr是常用的

    n 双刃剑,使用这个在特定的场景下资源消耗比纯生的new delete效率又损失

    文件资源泄露

    Ø df -k /##

    n 如果某个目录下的操作文件反复读写,可以看到正在使用和已经使用的信息,据此可以查看是否有文件没有关闭

    Ø Lsof

    n 列出当前系统打开文件的工具

    n lsof -p pid

    n http://blog.csdn.net/guoguo1980/article/details/2324454

    Ø fuser

    n 用来显示所有正在使用着指定的file, file system 或者 sockets的进程信息

    n http://www.cnblogs.com/yuboyue/archive/2011/07/18/2109838.html

    查看网络信息的netstat ss

    http://stackoverflow.com/questions/11763376/difference-between-netstat-and-ss-in-linux

    http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201222502510543/

    procfs

    windows下有sysinternals工具,非常方便监控widnows的文件、网络、注册表等的访问;Linux的更厉害的武器,不过都是文件,查看起来有点麻烦,用习惯了功能更强大。

    http://blog.csdn.net/jeek566/article/details/8695240

    mysql泄露

    mysql命令行可以初步查看,如下:

    root@csbit:~# mysql -uroot -proot

    Warning: Using a password on the command line interface can be insecure.

    Welcome to the MySQL monitor.  Commands end with ; or g.

    Your MySQL connection id is 28

    Server version: 5.6.21 MySQL Community Server (GPL)

    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its

    affiliates. Other names may be trademarks of their respective

    owners.

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql> show processlist;

    +----+-------+-----------+--------+---------+------+-------+------------------+

    | Id | User  | Host      | db     | Command | Time | State | Info             |

    +----+-------+-----------+--------+---------+------+-------+------------------+

    | 28 | root  | localhost | NULL   | Query   |    0 | init  | show processlist |

    +----+-------+-----------+--------+---------+------+-------+------------------+

    3 rows in set (0.00 sec)

    如果show processlist显示的数据不断增多,软件就是有链接泄露,c++处理最好使用cppdb这些有连接池处理的第三方库加速

    如果想检查更详细的信息MONyog这个工具可以一用

    崩溃分析

    泄露分析工具

    http://www.cnblogs.com/2018/p/3228174.html

    http://www.cnblogs.com/2018/p/3230736.html

    linux下发布的执行文件崩溃的问题定位 心得一则

    http://www.cnblogs.com/2018/p/3010691.html

    http://www.cnblogs.com/2018/archive/2012/05/18/2503897.html

    性能调优

    问题出来后,如何改善系统性能,此时切记8/2原则,首先修改的是关键路径的部分

    http://www.cnblogs.com/2018/p/3380773.html

  • 相关阅读:
    异常:java.io.IOException: Too many open files:
    转载 Servlet3.0中使用注解配置Servle
    Spring 源码从github导入源码到idea2016
    git 命令
    常用linux命令
    mysql优化常用语句
    mysql中in、not in、exists和not exists的区别
    mysql优化
    php常用的数据结构算法
    算法(一)
  • 原文地址:https://www.cnblogs.com/2018/p/4439484.html
Copyright © 2011-2022 走看看