{"id":2509,"date":"2016-06-26T00:00:11","date_gmt":"2016-06-25T23:00:11","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2509"},"modified":"2016-06-26T00:01:01","modified_gmt":"2016-06-25T23:01:01","slug":"malloc-info-fortran","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2509","title":{"rendered":"malloc info fortran"},"content":{"rendered":"<p>Code snipped to get detailed information about memory usage<\/p>\n<p><a href=\"http:\/\/roboblog.fatal-fury.de\/wp-content\/uploads\/2016\/06\/mallocinfo.f95.zip\"rel=\"\">mallocinfo.f95.zip<\/a><\/p>\n<p>Related: <a href=\"http:\/\/roboblog.fatal-fury.de\/?p=2485\">Code snipped to read \/proc status with fortran.<\/a><\/p>\n<pre><code>\r\nmodule MallocInfo_m\r\n  use :: iso_c_binding\r\n  implicit none\r\n  \r\n    !> This structure type is used to return information about the dynamic memory allocator.\r\n    type, bind(c) :: MallInfo_t\r\n      !> This is the total size of memory allocated with sbrk by malloc, in bytes.\r\n      integer(c_int) :: arena                  \r\n      !> This is the number of chunks not in use. (The memory allocator internally gets chunks of memory from the operating system, and then carves them up to satisfy individual malloc requests; see Efficiency and Malloc.)\r\n      integer(c_int) :: ordblks        \r\n      !> This field is unused.\r\n      integer(c_int) :: smblks        \r\n      !> This is the total number of chunks allocated with mmap.\r\n      integer(c_int) :: hblks        \r\n      !> This is the total size of memory allocated with mmap, in bytes.\r\n      integer(c_int) :: hblkhd        \r\n      !> This field is unused.\r\n      integer(c_int) :: usmblks        \r\n      !> This field is unused.\r\n      integer(c_int) :: fsmblks        \r\n      !> This is the total size of memory occupied by chunks handed out by malloc.\r\n      integer(c_int) :: uordblks        \r\n      !> This is the total size of memory occupied by free (not in use) chunks.\r\n      integer(c_int) :: fordblks        \r\n      !> This is the size of the top-most releasable chunk that normally borders the end of the heap (i.e., the high end of the virtual address space\u2019s data segment).\r\n      integer(c_int) :: keepcost      \r\n    end type\r\n    \r\n    interface\r\n      function mallinfo() bind(c, name=\"mallinfo\") result(data)\r\n        use :: iso_c_binding\r\n        implicit none\r\n        \r\n        type, bind(c) :: MallInfo_t      \r\n          integer(c_int) :: arena                        \r\n          integer(c_int) :: ordblks              \r\n          integer(c_int) :: smblks        \r\n          integer(c_int) :: hblks        \r\n          integer(c_int) :: hblkhd        \r\n          integer(c_int) :: usmblks        \r\n          integer(c_int) :: fsmblks        \r\n          integer(c_int) :: uordblks        \r\n          integer(c_int) :: fordblks        \r\n          integer(c_int) :: keepcost      \r\n        end type\r\n        type(MallInfo_t) :: data\r\n      end function\r\n    end interface\r\n  \r\n  contains\r\n  \r\n  subroutine getMallocInfo(malinfo)\r\n    implicit none\r\n    type(MallInfo_t), intent(out) :: malinfo\r\n    malinfo = mallinfo()\r\n  end subroutine\r\n  \r\n  subroutine printMallInfo(malinfo)\r\n    implicit none\r\n    type(MallInfo_t), intent(in) :: malinfo\r\n    write(*,*) \"Total size of memory allocated with sbrk by malloc in byte.  \", malinfo%arena\r\n    write(*,*) \"Total size of memory allocated with mmap, in bytes.          \", malinfo%hblkhd\r\n    write(*,*) \"Total size of memory occupied by chunks handed out by malloc.\", malinfo%uordblks\r\n    write(*,*) \"Total number of chunks allocated with mmap.                  \", malinfo%hblks\r\n    write(*,*) \"Number of chunks not in use.                                 \", malinfo%ordblks\r\n    write(*,*) \"Total size of memory occupied by free (not in use) chunks.   \", malinfo%fordblks\r\n    write(*,*) \"Size of the top-most releasable chunk borders end of the heap\", malinfo%keepcost    \r\n  end subroutine\r\n  \r\n  \r\nend module\r\n\r\nprogram test\r\n  use MallocInfo_m\r\n  implicit none\r\n  type(MallInfo_t) :: mallinfos(10000)  \r\n  integer :: i, nInfos\r\n  integer, allocatable :: data(:)\r\n  \r\n  allocate(data(0))\r\n  nInfos = 0\r\n  do i=1, 10\r\n    write(*,*) \"Iteration\",i\r\n    deallocate(data)\r\n    allocate(data(i*100000))\r\n    nInfos = nInfos+1\r\n    call getMallocInfo(mallinfos(nInfos))    \r\n    call printMallInfo(mallInfos(nInfos))\r\n    call sleep(1)\r\n  end do\r\n  \r\n   do i=10, 1, -1\r\n    write(*,*) \"Iteration\",i\r\n    deallocate(data)\r\n    allocate(data(i*100000))\r\n    nInfos = nInfos+1\r\n    call getMallocInfo(mallinfos(nInfos))    \r\n    call printMallInfo(mallInfos(nInfos))    \r\n    call sleep(1)\r\n  end do\r\n  \r\n  write(*,*) \"Total size of memory allocated with sbrk. min, mean, max\", minval(mallinfos(1:nInfos)%arena), sum(mallinfos(1:nInfos)%arena)\/nInfos, maxval(mallinfos(1:nInfos)%arena)\r\nend program\r\n<\/code><\/pre>\n<blockquote><p>\n.\/a.out<br \/>\n Iteration           1<br \/>\n Total size of memory allocated with sbrk by malloc in byte.        135168<br \/>\n Total size of memory allocated with mmap, in bytes.                401408<br \/>\n Total size of memory occupied by chunks handed out by malloc.        7080<br \/>\n Total number of chunks allocated with mmap.                             1<br \/>\n Number of chunks not in use.                                            1<br \/>\n Total size of memory occupied by free (not in use) chunks.         128088<br \/>\n Size of the top-most releasable chunk borders end of the heap      128088<br \/>\n Iteration           2<br \/>\n Total size of memory allocated with sbrk by malloc in byte.        135168<br \/>\n Total size of memory allocated with mmap, in bytes.                802816<br \/>\n Total size of memory occupied by chunks handed out by malloc.        7080<br \/>\n Total number of chunks allocated with mmap.                             1<br \/>\n Number of chunks not in use.                                            1<br \/>\n Total size of memory occupied by free (not in use) chunks.         128088<br \/>\n Size of the top-most releasable chunk borders end of the heap      128088<br \/>\n.<br \/>\n.<br \/>\n.<br \/>\n Total size of memory allocated with sbrk. min, mean, max      135168      155443      540672\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Code snipped to get detailed information about memory usage mallocinfo.f95.zip Related: Code snipped to read \/proc status with fortran. module MallocInfo_m use :: iso_c_binding implicit none !> This structure type is used to return information about the dynamic memory allocator. type, bind(c) :: MallInfo_t !> This is the total size of memory allocated with sbrk [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[30],"class_list":["post-2509","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-fortran"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2509"}],"version-history":[{"count":6,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2509\/revisions"}],"predecessor-version":[{"id":2516,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2509\/revisions\/2516"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2509"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}