Query regd. memory usage

Discussion in 'Java' started by Sunil, May 9, 2007.

  1. Sunil

    Sunil Guest

    Hi ,

    I have a method which takes a list of beans as parameter and does some
    insertions into the database. I have something like this inside the
    for loop:

    for(int i=0;i<list.size();i++){
    MyBean bean = new MyBean();
    ....................
    ....................
    }


    Initializing the bean inside the for loop is mandatory.
    Will initializing the bean inside the for loop cause more memory
    usage?
    Or is this better?

    MyBean bean = null;
    for(int i=0;i<list.size();i++){
    bean = new MyBean();
    ....................
    ....................
    }
    Which of the above two constructs is better?
    My guess is both of them consume the same amount of memory. And there
    is no difference in the performance either.

    What do you think? Your suggestions are welocme.

    Thanks & Regards,
    Sunil.
     
    Sunil, May 9, 2007
    #1
    1. Advertising

  2. Andrew Thompson, May 9, 2007
    #2
    1. Advertising

  3. "Sunil" <> wrote in message
    news:...
    | Hi ,
    |
    | I have a method which takes a list of beans as parameter and does some
    | insertions into the database. I have something like this inside the
    | for loop:
    |
    | for(int i=0;i<list.size();i++){
    | MyBean bean = new MyBean();
    | ....................
    | ....................
    | }
    |
    |
    | Initializing the bean inside the for loop is mandatory.
    | Will initializing the bean inside the for loop cause more memory
    | usage?
    | Or is this better?
    |
    | MyBean bean = null;
    | for(int i=0;i<list.size();i++){
    | bean = new MyBean();
    | ....................
    | ....................
    | }
    | Which of the above two constructs is better?
    | My guess is both of them consume the same amount of memory. And there
    | is no difference in the performance either.

    Your guess is right--there is virtually no difference to the two
    constructions because both allocate the same number of bean objects--the
    location of the declaration does not affect the time or memory.
    Furthermore, if you are inserting into the database on each pass through the
    loop, the network and disk traffic will far exceed anything you do in the
    program.

    If you determine through profiling that the list and MyBean are so large
    that they're a problem, you could get allocate only one and reuse it.

    MyBean bean = new MyBean ();
    for (int i = 0; i < list.size(); i++) {
    // set the full bean contents, apply and insert
    }

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, May 9, 2007
    #3
  4. Sunil

    Lew Guest

    Matt Humphrey wrote:
    > "Sunil" <> wrote in message
    > news:...
    > | Hi ,
    > |
    > | I have a method which takes a list of beans as parameter and does some
    > | insertions into the database. I have something like this inside the
    > | for loop:
    > |
    > | for(int i=0;i<list.size();i++){
    > | MyBean bean = new MyBean();
    > | ....................
    > | ....................
    > | }
    > |
    > |
    > | Initializing the bean inside the for loop is mandatory.
    > | Will initializing the bean inside the for loop cause more memory
    > | usage?
    > | Or is this better?
    > |
    > | MyBean bean = null;
    > | for(int i=0;i<list.size();i++){
    > | bean = new MyBean();
    > | ....................
    > | ....................
    > | }
    > | Which of the above two constructs is better?
    > | My guess is both of them consume the same amount of memory. And there
    > | is no difference in the performance either.
    >
    > Your guess is right--there is virtually no difference to the two
    > constructions because both allocate the same number of bean objects--the
    > location of the declaration does not affect the time or memory.
    > Furthermore, if you are inserting into the database on each pass through the
    > loop, the network and disk traffic will far exceed anything you do in the
    > program.
    >
    > If you determine through profiling that the list and MyBean are so large
    > that they're a problem, you could get allocate only one and reuse it.
    >
    > MyBean bean = new MyBean ();
    > for (int i = 0; i < list.size(); i++) {
    > // set the full bean contents, apply and insert
    > }


    The decision of where to declare a variable, or where to reuse it, should not
    be done on the basis of putative performance gains. You will get more by
    tuning the garbage collector than by detuning an algorithm.

    The decision of how to use a variable in your program should be based on
    correctness, such as the proper scope for it.

    Declaring the "bean" variable inside the loop limits its scope to the loop.
    Declaring it outside the loop gives it wider scope. Wider scope can be bad if
    it is too wide; it couples program segments together that should be independent.

    Reuse can have unintended consequences, especially if you are careless about
    clearing "old" data before reusing an object. Long-lived objects are GCed
    differently from the kind of quick in-the-loop objects being discussed here.
    Young-generation collections are very fast, since dead objects don't take GC
    time and most of the objects in the "within-loop" declaration style will be dead.

    Stop worrying about optimization and concentrate on correctness. Stick with
    the within-the-loop declaration and re-allocation in each iteration, unless
    scope demands that the variable be seen elsewhere.


    --
    Lew
     
    Lew, May 9, 2007
    #4
  5. Sunil

    Sunil Guest

    >
    > The decision of where to declare a variable, or where to reuse it, should not
    > be done on the basis of putative performance gains. You will get more by
    > tuning the garbage collector than by detuning an algorithm.
    >
    > The decision of how to use a variable in your program should be based on
    > correctness, such as the proper scope for it.
    >
    > Declaring the "bean" variable inside the loop limits its scope to the loop.
    > Declaring it outside the loop gives it wider scope. Wider scope can be bad if
    > it is too wide; it couples program segments together that should be independent.
    >
    > Reuse can have unintended consequences, especially if you are careless about
    > clearing "old" data before reusing an object. Long-lived objects are GCed
    > differently from the kind of quick in-the-loop objects being discussed here.
    > Young-generation collections are very fast, since dead objects don't take GC
    > time and most of the objects in the "within-loop" declaration style will be dead.
    >
    > Stop worrying about optimization and concentrate on correctness. Stick with
    > the within-the-loop declaration and re-allocation in each iteration, unless
    > scope demands that the variable be seen elsewhere.
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    Thanks Lew and Matt for your valuable responses.
     
    Sunil, May 10, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metfan
    Replies:
    2
    Views:
    4,874
    Robert Olofsson
    Oct 21, 2003
  2. frame
    Replies:
    2
    Views:
    331
    frame
    Sep 25, 2006
  3. frame
    Replies:
    1
    Views:
    354
    Salt_Peter
    Dec 11, 2006
  4. hvt
    Replies:
    0
    Views:
    1,229
  5. hvt
    Replies:
    0
    Views:
    1,514
Loading...

Share This Page