Fortran Coder

查看: 17711|回复: 9
打印 上一主题 下一主题

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

[复制链接]

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

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

回帖奖励 +5

[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

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 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

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

板凳
发表于 2018-11-3 09:16:54 | 显示全部楼层
这个就是不准,没办法。内存的占用,从原始内存到操作系统内存管理,映射成平坦模式,是个复杂的问题。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-3 02:42

Powered by Tencent X3.4

© 2013-2024 Tencent

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