Fortran Coder

查看: 86|回复: 6

[求助] 如何在程序内输出程序所占用内存大小

[复制链接]
回帖奖励 5 元F 币 回复本帖可获得 5 元F 币奖励! 每人限 1 次

22

帖子

9

主题

0

精华

熟手

F 币
115 元
贡献
161 点
发表于 2017-11-13 11:28:27 | 显示全部楼层 |阅读模式
环境:vs2010+ivf2013背景:程序存在内存泄露

问题:采用何种方式,输出fortran程序的内存占用情况?调用外部命令.or. ifot的特有模块?


回复

使用道具 举报

1148

帖子

12

主题

5

精华

论坛跑堂

Fcode跑堂

F 币
1128 元
贡献
892 点

新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2017-11-13 12:51:12 | 显示全部楼层

回帖奖励 +5 元F 币

[Fortran] 纯文本查看 复制代码
Program Main
  use Kernel32
  use ISO_C_Binding 
  Implicit None
  type(T_MEMORYSTATUSEX) stMemStat
  integer i
  stMemStat%dwLength = c_sizeof(stMemStat)
  i = GlobalMemoryStatusEx ( stMemStat )
  write(*,*) "There is  ",stMemStat%dwMemoryLoad,"% percent of memory in use."
  write(*,*) "There are ",stMemStat%ullTotalPhys/1024," total KB of physical memory."
  write(*,*) "There are ",stMemStat%ullAvailPhys/1024," free  KB of physical memory."
  write(*,*) "There are ",stMemStat%ullTotalPageFile/1024," total KB of paging file."
  write(*,*) "There are ",stMemStat%ullAvailPageFile/1024," free  KB of paging file."
  write(*,*) "There are ",stMemStat%ullTotalVirtual/1024," total KB of virtual memory."
  write(*,*) "There are ",stMemStat%ullAvailVirtual/1024," free  KB of virtual memory."
  write(*,*) "There are ",stMemStat%ullAvailExtendedVirtual/1024," free  KB of extended memory."
End Program Main

376

帖子

2

主题

0

精华

大宗师

F 币
2585 元
贡献
1549 点

水王勋章元老勋章热心勋章

发表于 2017-11-13 14:08:46 | 显示全部楼层

回帖奖励 +5 元F 币

本帖最后由 pasuka 于 2017-11-13 14:13 编辑

ivf的官方解决方案之一:
https://software.intel.com/en-us ... -intel-inspector-xe
Intel Inspector XE provides two main categories of memory usage information about an application:

Memory growth information about memory that has been allocated, but not yet freed
Memory leak information about memory that has been allocated but can no longer be freed because the application has lost all references to it

下载网址
https://software.intel.com/en-us/intel-inspector-xe

高校或非盈利科研院所建议掏钱买正版,全套ivf单机版折扣价不会超过北上广深的法定最低月工资的5倍

22

帖子

9

主题

0

精华

熟手

F 币
115 元
贡献
161 点
 楼主| 发表于 2017-11-13 14:39:08 | 显示全部楼层
fcode 发表于 2017-11-13 12:51
[mw_shl_code=fortran,true]Program Main
  use Kernel32
  use ISO_C_Binding

非常感谢!
测试了下代码,有些疑问:
1.我的T_MEMORYSTATUSEX的type中ullTotalPhys相关参数存在lowpart和highpart子参数。
例如stMemStat%ullTotalPhys%lowpart和stMemStat%ullTotalPhys%highpart分别是指什么意思?
2.debug测试中stMemStat%ullTotalPhys%lowpart存在负值,是不是采用c接口的同时,这些参数是无符号数被强制转换了?
3.我的目标是查看本程序的内存量,是不是用ullTotalVirtual-ullTotalVirtual即可?

22

帖子

9

主题

0

精华

熟手

F 币
115 元
贡献
161 点
 楼主| 发表于 2017-11-13 14:52:22 | 显示全部楼层
pasuka 发表于 2017-11-13 14:08
ivf的官方解决方案之一:
https://software.intel.com/en-us ... -intel-inspector-xe

我测试过ivf2017版本的inspector,并不能完全解决memory leak的问题,有些问题还得自己找出来。

376

帖子

2

主题

0

精华

大宗师

F 币
2585 元
贡献
1549 点

水王勋章元老勋章热心勋章

发表于 2017-11-13 16:20:03 | 显示全部楼层
ksfengjia 发表于 2017-11-13 14:52
我测试过ivf2017版本的inspector,并不能完全解决memory leak的问题,有些问题还得自己找出来。 ...

涉及C、C++混合编程的话,好多坑隐藏得很深,一时半会儿爬不出来。。。

1148

帖子

12

主题

5

精华

论坛跑堂

Fcode跑堂

F 币
1128 元
贡献
892 点

新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2017-11-13 17:12:59 | 显示全部楼层
对不起,我刚刚在 x64 编译器上测试的。

32位的编译器的话,可能有点问题。Intel 自己提供的Kernel32库里,32位的结构用的一个2个4字节的成员的 type 带表示1个8字节的整数,比较麻烦。

你可以自己定义一个 My_MEMORYSTATUSEX,然后调用时给一个 T_MEMORYSTATUSEX,然后调用完转换成 My_MEMORYSTATUSEX 使用。

这些成员的含义,见以下注释。
[Fortran] 纯文本查看 复制代码
Program Main
  use Kernel32
  use ISO_C_Binding 
  Implicit None
  TYPE My_MEMORYSTATUSEX
   SEQUENCE
    integer(DWORD) dwLength ! knowns  DWORD 
    integer(DWORD) dwMemoryLoad ! knowns  DWORD 
    integer(DWORDLONG) ullTotalPhys ! knowns  DWORDLONG 
    integer(DWORDLONG) ullAvailPhys ! knowns  DWORDLONG 
    integer(DWORDLONG) ullTotalPageFile ! knowns  DWORDLONG 
    integer(DWORDLONG) ullAvailPageFile ! knowns  DWORDLONG 
    integer(DWORDLONG) ullTotalVirtual ! knowns  DWORDLONG 
    integer(DWORDLONG) ullAvailVirtual ! knowns  DWORDLONG 
    integer(DWORDLONG) ullAvailExtendedVirtual ! knowns  DWORDLONG   
  END TYPE
  type(T_MEMORYSTATUSEX)  stMemStat
  type(My_MEMORYSTATUSEX) stMyMemStat  
  integer i
  stMemStat%dwLength = c_sizeof(stMemStat)
  i = GlobalMemoryStatusEx ( stMemStat )
  stMyMemStat = transfer( stMemStat , stMyMemStat )
  write(*,*) "There is  ",stMyMemStat%dwMemoryLoad,"% percent of memory in use."!//内存使用百分比
  write(*,*) "There are ",stMyMemStat%ullTotalPhys/1024," total KB of physical memory."!//物理内存总量
  write(*,*) "There are ",stMyMemStat%ullAvailPhys/1024," free  KB of physical memory."!//物理内存可用量
  write(*,*) "There are ",stMyMemStat%ullTotalPageFile/1024," total KB of paging file."!//虚拟内存(页面文件)总量
  write(*,*) "There are ",stMyMemStat%ullAvailPageFile/1024," free  KB of paging file."!//虚拟内存(页面文件)可用量
  write(*,*) "There are ",stMyMemStat%ullTotalVirtual/1024," total KB of virtual memory."!//虚拟地址总量,32位固定为2GB
  write(*,*) "There are ",stMyMemStat%ullAvailVirtual/1024," free  KB of virtual memory."!//虚拟地址可用量(可以认为这个代表可用值)
  write(*,*) "There are ",stMyMemStat%ullAvailExtendedVirtual/1024," free  KB of extended memory."
End Program Main
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

QQ|捐赠本站|Archiver|关于我们 About Us|QQ群|Fcode

GMT+8, 2017-12-18 15:06

Powered by Discuz! X3.2

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表