1 2 3 Previous Next 39 Replies Latest reply: Jul 1, 2009 6:20 PM by notivago RSS

    using an array of primitive

    843789
      How can I truncate an array of (primitive) double?
      Is it like the following, or some more obviously legitimate way?
      double[] dArr=new double[12345678]; 
      dArr = Arrays.copyOf(dArr,7234567); 
      I don't want to have both sizes allocated at the same time.
      Also, (not as important to me) can I extend this array?

      And this reminds me of another related question:
      Why can't java allocate heap space by itself without my
      having to use, e.g., -Xmx999m manually and guessingly?
      Or can it?
      Or am I missing some procedure?
        • 1. Re: using an array of primitive
          843789
          hillmi wrote:
          How can I truncate an array of (primitive) double?
          Is it like the following, or some more obviously legitimate way?
          double[] dArr=new double[12345678]; 
          dArr = Arrays.copyOf(dArr,7234567); 
          I don't want to have both sizes allocated at the same time.
          Also, (not as important to me) can I extend this array?
          Extend NO.
          Make a new one YES, but both would be in memory at the same time.

          You could use an ArrayList with Double, then you could extend it.
          And this reminds me of another related question:
          Why can't java allocate heap space by itself without my
          having to use, e.g., -Xmx999m manually and guessingly?
          I do not believe it can.
          • 2. Re: using an array of primitive
            843789
            Regarding heap space,
            how can I determine how much heap space is available "now"?

            Regarding arrays, does the array of Double take much more
            space than an array of double of the same number of items?

            And does Double slow my program down?
            • 3. Re: using an array of primitive
              796365
              hillmi wrote:
              Regarding heap space,
              how can I determine how much heap space is available "now"?
              The Runtime class has methods for free, total, and max memory.
              >
              Regarding arrays, does the array of Double take much more
              space than an array of double of the same number of items?

              And does Double slow my program down?
              I recommend that you not worry about these questions until they actually result in a problem.
              • 4. Re: using an array of primitive
                796447
                hillmi wrote:
                Regarding arrays, does the array of Double take much more
                space than an array of double of the same number of items?

                And does Double slow my program down?
                Dear premature optimizer,

                You're already using much more memory than you really need, and your program is already slowed down considerably, because you are using Java and its accompanying platform.

                So I recommend you just scrap the current progress and rewrite it in Assembler.
                </sarcasm>
                • 5. Re: using an array of primitive
                  843789
                  Those last two answers are two ways of avoiding answering the questions.
                  Another way would be to give a partial answer if you know one,
                  or don't answer at all. Just leave it to someone who knows.
                  Yes I know the problems exist.
                  • 6. Re: using an array of primitive
                    843789
                    Thanks for the reference.
                    • 7. Re: using an array of primitive
                      796365
                      hillmi wrote:
                      Those last two answers are two ways of avoiding answering the questions.
                      Another way would be to give a partial answer if you know one,
                      or don't answer at all. Just leave it to someone who knows.
                      Yes I know the problems exist.
                      Then post some code samples that exhibit the "problems" instead of asking general questions that don't have real-world relevance.
                      • 8. Re: using an array of primitive
                        843789
                        Sorry, ChuckBing, I was aiming at warnerja.
                        I used Runtime to which you referred in order to show that
                        indeed it seems that the copy is in memory along side the
                        original before the name gets copied over
                        and the original garbaged.
                        I also noticed that it seems to allocate against
                        maxMemory instead of totalMemory or
                        freeMemory as Runtime seems to imply.
                        It is unfortunate that arrays cannot be truncated.
                        I believe I remember programming in some other older
                        language that supported it, and it seems like an
                        easy thing for Sun to rectify. Or maybe there is some
                        security reason it cannot be done.
                        • 9. Re: using an array of primitive
                          notivago
                          hillmi wrote:
                          Those last two answers are two ways of avoiding answering the questions.
                          Another way would be to give a partial answer if you know one,
                          or don't answer at all. Just leave it to someone who knows.
                          Yes I know the problems exist.
                          Nope, they are pointing you to the right direction. The reason the virtual machine is there is to take away those kind of worries from the developer, if you are doing java you should no be meddling with memory mannagement, unless due to some very special need risen by realworld situation. If you go down the "I want to take control of everything" path you are very likely to miss the strong points of java entirely.

                          Regards.
                          • 10. Re: using an array of primitive
                            843789
                            hillmi wrote:
                            I was aiming at warnerja.
                            Actually, if you got past the sarcasm, warnerja post was informative. He was bascially saying you are wasting your time in trying to make your code not waste time. You should be concentrating on writing your code. When you run it and discover that it takes 3 hours to execute then you can start worrying about optimisation.
                            • 11. Re: using an array of primitive
                              796365
                              It is unfortunate that arrays cannot be truncated.
                              I believe I remember programming in some other older
                              language that supported it, and it seems like an
                              easy thing for Sun to rectify. Or maybe there is some
                              security reason it cannot be done.
                              The original array will be garbage collected when needed by Java's automatic garbage collector as soon as all references to it disappear. That's why lack of truncation isn't important, and why you shouldn't try to prematurely optimize - Java can usually optimize better than you will., and won't unnecessarily spend resources doing it.
                              • 12. Re: using an array of primitive
                                843789
                                Replies 9,10,11 are only a little relevant, mostly irrelevant.
                                The situation seems to be that if you want to truncate arrays
                                of primitives, then there will have to be room for the original and
                                the copy (for a short time) even if you use the same name.
                                From my point of view this should be overcome by Sun
                                unless it makes for some insecurity.
                                • 13. Re: using an array of primitive
                                  843789
                                  hillmi wrote:
                                  From my point of view this should be overcome by Sun
                                  Why?

                                  Just becuase you don't understand something, why should Sun have to change how Java does something? Imagine you have a piece of paper with some numbers written on it. You then want to change those numbers (say by doubling them) and use the same piece of paper to write them on. Before you can do that you have to erase all the numbers from the piece of paper. Now what were those numbers?
                                  • 14. Re: using an array of primitive
                                    843789
                                    Look again.
                                    I'm not changing any numbers.
                                    I'm only wanting to tear off the unused
                                    part of the paper in a neat way.
                                    Hope that helps your understanding.
                                    But enough is enough.
                                    I will appreciate further informative posts.
                                    1 2 3 Previous Next