Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What's about the size of memory usage?

What's about the size of memory usage?

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nguyenvhn
    wrote on last edited by
    #1

    I have written a program, in which, I declared a global variable such as: typedef struct{ char buff[20]; } my_struct; my_struct my_arr[1000][30][10][10][3]; In particular: the size of my_struct is 20 bytes. My calculation showed that the size of my_arr variable is: 1000*30*10*10*3*20=180.000.000 bytes or close to 180M. But when I run that program, Task Manager showed me the program took only 3MB (in Mem Usage column). When I started to fill data into my_arr, Mem Usage increased very quickly and program terminated abnormally. Does anyone explain me about that situation? I used VC6 and MFC. Thanks for reading.

    P A P 3 Replies Last reply
    0
    • N nguyenvhn

      I have written a program, in which, I declared a global variable such as: typedef struct{ char buff[20]; } my_struct; my_struct my_arr[1000][30][10][10][3]; In particular: the size of my_struct is 20 bytes. My calculation showed that the size of my_arr variable is: 1000*30*10*10*3*20=180.000.000 bytes or close to 180M. But when I run that program, Task Manager showed me the program took only 3MB (in Mem Usage column). When I started to fill data into my_arr, Mem Usage increased very quickly and program terminated abnormally. Does anyone explain me about that situation? I used VC6 and MFC. Thanks for reading.

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      May be it was optimised by the compiler and may be windows too. so as and when you started filling up the data memory was allocated to it.. nguyenvhn wrote: When I started to fill data into my_arr, Mem Usage increased very quickly and program terminated abnormally. may be you overshot the buffer.


      God is Real, unless declared Integer.

      1 Reply Last reply
      0
      • N nguyenvhn

        I have written a program, in which, I declared a global variable such as: typedef struct{ char buff[20]; } my_struct; my_struct my_arr[1000][30][10][10][3]; In particular: the size of my_struct is 20 bytes. My calculation showed that the size of my_arr variable is: 1000*30*10*10*3*20=180.000.000 bytes or close to 180M. But when I run that program, Task Manager showed me the program took only 3MB (in Mem Usage column). When I started to fill data into my_arr, Mem Usage increased very quickly and program terminated abnormally. Does anyone explain me about that situation? I used VC6 and MFC. Thanks for reading.

        A Offline
        A Offline
        Abin
        wrote on last edited by
        #3

        By my_struct my_arr[1000][30][10][10][3]; you are allocating memory in the stack, and I don't think the OS will allow your application to allocate that much(180MB) stack memory, so definitely it was going to be terminated abnormally. What I don't understand is why it did not crash right away... Yeah, I too hope some gurus explain it.

        B 1 Reply Last reply
        0
        • A Abin

          By my_struct my_arr[1000][30][10][10][3]; you are allocating memory in the stack, and I don't think the OS will allow your application to allocate that much(180MB) stack memory, so definitely it was going to be terminated abnormally. What I don't understand is why it did not crash right away... Yeah, I too hope some gurus explain it.

          B Offline
          B Offline
          Behzad Ebrahimi
          wrote on last edited by
          #4

          How many are limitation of stack memory!? And if we need huge memory in global, how can we do it?

          M A 2 Replies Last reply
          0
          • B Behzad Ebrahimi

            How many are limitation of stack memory!? And if we need huge memory in global, how can we do it?

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            dynamic memory allocation.


            Maximilien Lincourt Your Head A Splode - Strong Bad

            1 Reply Last reply
            0
            • B Behzad Ebrahimi

              How many are limitation of stack memory!? And if we need huge memory in global, how can we do it?

              A Offline
              A Offline
              Abin
              wrote on last edited by
              #6

              How many are limitation of stack memory!? I think the stack memory usage per process vary throughout OS's but 180MB is unlikely possible. And if we need huge memory in global, how can we do it? uh?...You will of course have to allocate them from the heap.

              R 1 Reply Last reply
              0
              • A Abin

                How many are limitation of stack memory!? I think the stack memory usage per process vary throughout OS's but 180MB is unlikely possible. And if we need huge memory in global, how can we do it? uh?...You will of course have to allocate them from the heap.

                R Offline
                R Offline
                Renjith Ramachandran
                wrote on last edited by
                #7

                but i heard in windows platform, every process can access up tp 4 GB of memeory. (2^32) ???? I want to change Myself..Can u help me? :)

                M P 2 Replies Last reply
                0
                • N nguyenvhn

                  I have written a program, in which, I declared a global variable such as: typedef struct{ char buff[20]; } my_struct; my_struct my_arr[1000][30][10][10][3]; In particular: the size of my_struct is 20 bytes. My calculation showed that the size of my_arr variable is: 1000*30*10*10*3*20=180.000.000 bytes or close to 180M. But when I run that program, Task Manager showed me the program took only 3MB (in Mem Usage column). When I started to fill data into my_arr, Mem Usage increased very quickly and program terminated abnormally. Does anyone explain me about that situation? I used VC6 and MFC. Thanks for reading.

                  P Offline
                  P Offline
                  Paul Ranson
                  wrote on last edited by
                  #8

                  Assuming 'global' means that my_arr is declared outside any function then the stack doesn't come into it. What's happening is that the executable is being marked as needing more than 180M of memory and this much is reserved in the virtual address space of the process, but until you write (or read....) that memory it isn't actually allocated for real, in some cases you will never be able to have it all as real memory at once, it will be paged in and out from the pagefile as required. This is transparent apart from the slowness. Anyway look at the 'VM Size' column in Task Manager (View->Select Columns, if necessary) I think you will find this shows a more likely amount. I cannot explain why your program terminated abnormally unless you incorrectly accessed the array or your pagefile isn't big enough. Perhaps you could extract a small sample of the code you're using to access the array? I suspect you have an error in parameter order to that unwieldy array and are writing off the end. Anyway the following code runs to completion and you end up with a 'mem usage' of 180M.

                  typedef struct{
                  char buff[20];
                  } my_struct;
                  my_struct my_arr[1000][30][10][10][3];

                  int main()
                  {
                  for ( int a = 0; a < 1000; ++a )
                  for ( int b = 0; b < 30; ++b )
                  for ( int c = 0; c < 10; ++c )
                  for ( int d = 0; d < 10; ++d )
                  for ( int e = 0; e < 3; ++e )
                  {
                  my_arr [ a ][ b ][ c ][ d ][ e ].buff [ 0 ] = '0' ;
                  my_arr [ a ][ b ][ c ][ d ][ e ].buff [ 19 ] = '9' ;
                  }
                  return 0;
                  }

                  Paul

                  1 Reply Last reply
                  0
                  • R Renjith Ramachandran

                    but i heard in windows platform, every process can access up tp 4 GB of memeory. (2^32) ???? I want to change Myself..Can u help me? :)

                    M Offline
                    M Offline
                    Maximilien
                    wrote on last edited by
                    #9

                    yes but on/in the heap, not the stack ...


                    Maximilien Lincourt Your Head A Splode - Strong Bad

                    1 Reply Last reply
                    0
                    • R Renjith Ramachandran

                      but i heard in windows platform, every process can access up tp 4 GB of memeory. (2^32) ???? I want to change Myself..Can u help me? :)

                      P Offline
                      P Offline
                      Prakash Nadar
                      wrote on last edited by
                      #10

                      renjith_sree wrote: but i heard in windows platform, every process can access up tp 4 GB of memeory. (2^32) ???? I doubt its 4 gb, windows reserves 2gb kernel and 2gb for user applications.


                      God is Real, unless declared Integer.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups