代码分析工具中新增了六个 Discover API。当应用程序使用的内存太多时,可以在应用程序结束前使用这些 API 精确定位发生的内存泄漏。这些 API 还可以定位所有使用中的内存块。
可以将这些 API 直接插入到应用程序源代码中,也可以在调试会话过程中随时调用它们,以报告从某个点开始的新增内存使用量和内存使用总量,以及新增的内存泄漏量和内存泄漏总量。对于长时间运行的或者一直运行的企业服务器应用程序,这些 API 尤其有用。
代码分析工具中增加了以下 API:
discover_report_all_inuse()
discover_mark_all_inuse_as_reported()
discover_report_unreported_inuse()
discover_report_all_leaks()
discover_mark_all_leaks_as_reported()
discover_report_unreported_leaks()
以下是在使用 Discover 运行二进制文件以及使用 dbx 执行 API 之后使用 discover_report_unreported_leaks() API 的一个示例。
%discover -w - a.out % dbx a.out (dbx) stop in foo2 (dbx) run (dbx) call discover_report_unreported_leaks() ******** discover_report_unreported_leaks() Report ******** 1 allocation at 1 location left on the heap with a total size of 1 byte LEAK 1: 1 allocation with total size of 1 byte foo1() + 0x5e <api_example.c:7> 4: #include <discoverAPI.h> 5: 6: void foo1() { 7:=> char *x = (char *) malloc(sizeof(char)); 8: *x = 'a'; 9: printf("x = %c\n", *x); 10: /*free(x);*/ main() + 0x1a <api_example.c:21> 18: } 19: 20: int main() { 21:=> foo1(); 22: foo2(); 23: return 0; 24: } _start() + 0x71 **********************************************************
有关每个 Discover API 以及如何使用它们的更多信息,请参见 Discover 头文件以及Oracle Solaris Studio 12.4:Discover 和 Uncover 用户指南 中的discover API。