9 Replies Latest reply: Feb 4, 2011 9:19 AM by 835647 RSS

    Estimate memory usage?

    801937
      Dear all,

      I have array contains 10 000 000 object Student

      class Student{
      int id;
      }

      Student[] array = new Student[length];
      for(int i = 0; i< length; i++){
      array[i] = new Student();
      }

      I estimate my program will consume 190MB.
      That is: 8 + 4 + length*4 + 16*length

      Could you give me your estimation? And how you estimate that?

      Thanks.
        • 1. Re: Estimate memory usage?
          tschodt
          JPrefer wrote:
          Could you give me your estimation? And how you estimate that?
          Depends on the JVM implementation.
          Is it a Sun JVM?
          Is it a 32-bit implementation or a 64-bit implementation?
          • 2. Re: Estimate memory usage?
            801937
            I use SUN JVM in 32 bit system.
            • 3. Re: Estimate memory usage?
              tschodt
              What version of Sun (Oracle) JVM?
              On what underlying Operating System?
              • 4. Re: Estimate memory usage?
                801937
                My system is Ubuntu 10.04
                Java version is:
                java version "1.6.0_18"
                OpenJDK Runtime Environment (IcedTea6 1.8.2) (6b18-1.8.2-4ubuntu2)
                OpenJDK Server VM (build 16.0-b13, mixed mode)
                • 5. Re: Estimate memory usage?
                  tschodt
                  JPrefer wrote:
                  I have array contains 10 000 000 object Student

                  class Student{
                  int id;
                  }

                  Student[] array = new Student[length];
                  for(int i = 0; i< length; i++){
                  array[i] = new Student();
                  }

                  I estimate my program will consume 190MB.
                  That is: 8 + 4 + length*4 + 16*length
                  Could you give me your estimation?
                  On CentOS 5.5 64-bit with Sun 64-bit VM 1.6.0_18-b07, I estimate 420m
                  And how you estimate that?
                  class JPrefer {
                      static class Student {
                          int id;
                      }
                  
                      static void memory(String s) {
                          Runtime rt = Runtime.getRuntime();
                          rt.gc();
                          System.out.println(s+
                              ":0x"+Long.toHexString(rt.maxMemory())+
                              ":0x"+Long.toHexString(rt.totalMemory())+
                              ":0x"+Long.toHexString(rt.freeMemory()));
                      }
                      public static void main(String[] arg) throws Exception {
                          int length = Integer.decode(arg[0]);
                          Student[] array;
                          for(int x=0;x<100;++x) {
                              memory("A");
                              array = new Student[length];
                              for(int i = 0; i< length; i++){
                                try {
                                  array[i] = new Student();
                                } catch (OutOfMemoryError ex) {
                                  array = null;
                                  System.err.println("I: "+i+", 0x"+Integer.toHexString(i));
                                  ex.printStackTrace();
                                  return;
                                }
                              }
                              memory("B");
                              array = null;
                          }
                      }
                  }
                  $ java -Xmx419m JPrefer 10000000
                  A:0x17560000:0x3770000:0x36fdb70
                  I: 9866290, 0x968c32
                  java.lang.OutOfMemoryError: Java heap space
                          at JPrefer.main(JPrefer.java:22)
                  $ java -Xmx420m JPrefer 10000000
                  • 6. Re: Estimate memory usage?
                    802316
                    On my 64-bit JVM using "-XX:+UseCompressedOops -XX:NewSize=1g -verbosegc"
                        static class Student {
                            int id;
                        }
                    
                        public static void main(String... args) throws InterruptedException {
                            long free = Runtime.getRuntime().freeMemory();
                            int length = 10 * 1000 * 1000;
                            Student[] array = new Student[length];
                            for (int i = 0; i < length; i++) {
                                array[i] = new Student();
                            }
                            long used = free - Runtime.getRuntime().freeMemory();
                            System.out.println("used = "+used/1024/1024+" MB");
                        }
                    prints (without any GCs)
                    used = 176 MB
                    Your estimate looks fine to me.
                    • 7. Re: Estimate memory usage?
                      835647
                      On Java 1.6 32bit it requires Xmx225m
                      So 225 MegaBytes.

                      The array is filled within 3.5 sec.

                      This is because each Object requires some extra bytes.

                      In Ansi C or Pascal you could use struct or record, respectively.
                      This would take much less time and memory.

                      Christoph
                      • 8. Re: Estimate memory usage?
                        jschellSomeoneStoleMyAlias
                        user10289576 wrote:
                        In Ansi C or Pascal you could use struct or record, respectively.
                        This would take much less time and memory.
                        How is that relevant to a professional programmer?
                        • 9. Re: Estimate memory usage?
                          835647
                          It was a discussion in the past whether structs are needed or not.

                          Since object instantiation and gc is highly improved and RAM is cheap, I would tend to say that structs are not implemented.

                          Imagine, Java generates 1E7 Objects per second!

                          Java programs are potentially nearly as fast as gnu C.

                          Nevertheless,
                          certain number crunching algorithms that need high numbers of data structures would be slow and memory demanding
                          and are easier written in C.