Re: Do you use a garbage collector (java vs c++ difference in "new")

Discussion in 'Java' started by asterisc, Apr 11, 2008.

  1. asterisc

    asterisc Guest

    On Apr 11, 10:20 am, Razii <> wrote:
    > On Thu, 10 Apr 2008 20:37:59 -0500, Razii
    >
    > <> wrote:
    > >int main(int argc, char *argv[]) {

    >
    > > clock_t start=clock();
    > > for (int i=0; i<=10000000; i++) {
    > > Test *test = new Test(i);
    > > if (i % 5000000 == 0)
    > > cout << test;
    > > }

    >
    > If I add delete test; to this loop it gets faster. huh? what the
    > exaplanation for this?
    >
    > 2156 ms
    >
    > and after I add delete test; to the loop
    >
    > 1781 ms
    >
    > why is that?


    Probably because of some compiler's optimizations.

    When do you free memory in Java? Before or after you stop the clock?
     
    asterisc, Apr 11, 2008
    #1
    1. Advertising

  2. asterisc

    asterisc Guest

    On Apr 11, 10:43 am, Razii <> wrote:
    > On Fri, 11 Apr 2008 00:31:30 -0700 (PDT), asterisc <>
    > wrote:
    >
    > >When do you free memory in Java? Before or after you stop the clock?

    >
    > You don't. When an object is no longer referenced by the program, the
    > GC will recycled it. The space is made available for subsequent new
    > objects.


    Let me rephrase that:
    When exactly is the memory deallocated? Before or after you stop the
    clock?
    Do you know exactly when the GC will free the memory ?

    I think the memory for every new is not referenced outside the loop.
    Does GC run in every loop step?
    Can you print out a message like: "here is allocated at address: xxx"
    and "here is deallocated from address: xxx"

    What you said is pure theory. I'm worried about how is that in
    practice...
     
    asterisc, Apr 11, 2008
    #2
    1. Advertising

  3. asterisc

    asterisc Guest

    On Apr 11, 11:15 am, Razii <> wrote:
    > On Fri, 11 Apr 2008 00:58:30 -0700 (PDT), asterisc <>
    > wrote:
    >
    > >When exactly is the memory deallocated? Before or after you stop the
    > >clock?

    >
    > I already posted the output in the other post. The clock stops when
    > this is printed.
    >
    > long end = System.currentTimeMillis();
    > System.out.println("Time: " + (end - start) + " ms");
    >
    > The GC (in a different thread) runs several times while the main
    > thread is in the loop.
    >
    > Once the main() thread ends, all the memory is returned to the
    > operating system by the JVM anyway.
    >
    > Test@19f953d <----printed when i == 0
    > [GC 896K->108K(5056K), 0.0018061 secs]
    > [GC 1004K->108K(5056K), 0.0004453 secs]
    > [GC 1004K->108K(5056K), 0.0001847 secs]
    > [GC 1004K->108K(5056K), 0.0000500 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000506 secs]
    > [GC 1004K->108K(5056K), 0.0000483 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000497 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000955 secs]
    > [GC 1004K->108K(5056K), 0.0000494 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000531 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000534 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000534 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000592 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000601 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > Test@e86da0 <----printed when i == 5000000
    > [GC 1004K->108K(5056K), 0.0000528 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000545 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000489 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000612 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000902 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000601 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000503 secs]
    > [GC 1004K->108K(5056K), 0.0000483 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000623 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000673 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000623 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000514 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > Test@1975b59 <----printed when i == 10000000
    > Time: 172 ms


    Can you print out a message like: "here is allocated at address: xxx"
    and "here is deallocated from address: xxx"
     
    asterisc, Apr 11, 2008
    #3
  4. asterisc

    asterisc Guest

    On Apr 11, 11:15 am, Razii <> wrote:
    > On Fri, 11 Apr 2008 00:58:30 -0700 (PDT), asterisc <>
    > wrote:
    >
    > >When exactly is the memory deallocated? Before or after you stop the
    > >clock?

    >
    > I already posted the output in the other post. The clock stops when
    > this is printed.
    >
    > long end = System.currentTimeMillis();
    > System.out.println("Time: " + (end - start) + " ms");
    >
    > The GC (in a different thread) runs several times while the main
    > thread is in the loop.
    >
    > Once the main() thread ends, all the memory is returned to the
    > operating system by the JVM anyway.
    >
    > Test@19f953d <----printed when i == 0
    > [GC 896K->108K(5056K), 0.0018061 secs]
    > [GC 1004K->108K(5056K), 0.0004453 secs]
    > [GC 1004K->108K(5056K), 0.0001847 secs]
    > [GC 1004K->108K(5056K), 0.0000500 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000506 secs]
    > [GC 1004K->108K(5056K), 0.0000483 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000497 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000955 secs]
    > [GC 1004K->108K(5056K), 0.0000494 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000531 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000534 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000534 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000592 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000601 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > Test@e86da0 <----printed when i == 5000000
    > [GC 1004K->108K(5056K), 0.0000528 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000545 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000489 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000612 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000902 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000601 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000503 secs]
    > [GC 1004K->108K(5056K), 0.0000483 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000623 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000673 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000623 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000514 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > Test@1975b59 <----printed when i == 10000000
    > Time: 172 ms


    Can you print out a message like: "here is allocated at address: xxx"
    and "here is deallocated from address: xxx"
     
    asterisc, Apr 11, 2008
    #4
  5. asterisc

    asterisc Guest

    On Apr 11, 11:15 am, Razii <> wrote:
    > On Fri, 11 Apr 2008 00:58:30 -0700 (PDT), asterisc <>
    > wrote:
    >
    > >When exactly is the memory deallocated? Before or after you stop the
    > >clock?

    >
    > I already posted the output in the other post. The clock stops when
    > this is printed.
    >
    > long end = System.currentTimeMillis();
    > System.out.println("Time: " + (end - start) + " ms");
    >
    > The GC (in a different thread) runs several times while the main
    > thread is in the loop.
    >
    > Once the main() thread ends, all the memory is returned to the
    > operating system by the JVM anyway.
    >
    > Test@19f953d <----printed when i == 0
    > [GC 896K->108K(5056K), 0.0018061 secs]
    > [GC 1004K->108K(5056K), 0.0004453 secs]
    > [GC 1004K->108K(5056K), 0.0001847 secs]
    > [GC 1004K->108K(5056K), 0.0000500 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000506 secs]
    > [GC 1004K->108K(5056K), 0.0000483 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000497 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000955 secs]
    > [GC 1004K->108K(5056K), 0.0000494 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000531 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000534 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000534 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000592 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000601 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > Test@e86da0 <----printed when i == 5000000
    > [GC 1004K->108K(5056K), 0.0000528 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000545 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000486 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000489 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    > [GC 1004K->108K(5056K), 0.0000612 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000902 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000601 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000503 secs]
    > [GC 1004K->108K(5056K), 0.0000483 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000623 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000673 secs]
    > [GC 1004K->108K(5056K), 0.0000461 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000458 secs]
    > [GC 1004K->108K(5056K), 0.0000478 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000623 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000472 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > [GC 1004K->108K(5056K), 0.0000469 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000475 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000453 secs]
    > [GC 1004K->108K(5056K), 0.0000481 secs]
    > [GC 1004K->108K(5056K), 0.0000514 secs]
    > [GC 1004K->108K(5056K), 0.0000455 secs]
    > Test@1975b59 <----printed when i == 10000000
    > Time: 172 ms


    Can you print out a message like: "here is allocated at address: xxx"
    and "here is deallocated from address: xxx"
     
    asterisc, Apr 11, 2008
    #5
  6. asterisc

    asterisc Guest

    On Apr 11, 12:43 pm, Razii <> wrote:
    > On Fri, 11 Apr 2008 01:18:30 -0700 (PDT), asterisc <>
    > wrote:
    >
    > >Can you print out a message like: "here is allocated at address: xxx"
    > >and "here is deallocated from address: xxx"

    >
    > What would be the point? Everything is clear here.
    >
    > [GC 1004K->108K(5056K), 0.0000464 secs]
    >
    > GC == Garbage Collection.
    > 1004K == Heap in use before collection.
    > 108K == heap in use after collection.
    > 5056K == Total heap size
    >
    > 0.0000464 secs == time it took.
    >
    > There were a total of 174 of these outputs. That means,
    >
    > 0.0000464 * 174 = 0.0080736 sec == 8 ms
    >
    > GC took 8 ms total...


    What's the point allocating 1.000.000 new objects and never use them?

    Why doesn't Java optimize this and allocate nothing, since they are
    not used?

    Anyway, in C++ it's a good idea to create memory pools if you intend
    to play with a lot of small objects. Something like Ian did.

    Try creating a memory pool in Java and compare it with GC's
    performance.

    Can you overwrite the new operator in Java?
    Or are you comparing std::new with Java's new?

    You forget that Java's GC is implemented in a lower-level programming
    language, as C or C++. It cannot be faster than an implementation in C/
    C++.
     
    asterisc, Apr 11, 2008
    #6
  7. asterisc

    Mark Space Guest

    asterisc wrote:

    > Why doesn't Java optimize this and allocate nothing, since they are
    > not used?


    An interesting idea. Responding to your earlier request for an output
    line for each deletion, I added a finalize method. Then I cut down the
    number of loops to 1000, and ran it. I got this:

    init:
    deps-jar:
    Compiling 1 source file to
    C:\Users\Brenden\Dev\misc\FinalizeTest\build\classes
    compile:
    run:
    finalizetest.Main@1eed786
    Time: 2 ms
    BUILD SUCCESSFUL (total time: 0 seconds)


    Hmmm..... although increasing the loops does produce more output,
    increasing the loops to one million only produces ~7k lines of total output.


    Code below:


    package finalizetest;

    public class Main
    {

    private static final int LOOPS = 1000;
    private static final int MODULUS = 5000000;

    Main( int c )
    {
    count = c;
    }

    int count;

    public static void main( String[] arg )
    {

    long start = System.currentTimeMillis();

    for ( int i = 0; i < LOOPS; i++ )
    {
    Main test = new Main( i );
    if ( i % MODULUS == 0 )
    {
    System.out.println( test );
    }
    }
    long end = System.currentTimeMillis();
    System.out.println( "Time: " + ( end - start ) + " ms" );

    }

    @Override
    protected void finalize() throws Throwable
    {
    System.out.println( "Finalized: " + this );
    super.finalize();
    }
    }
     
    Mark Space, Apr 12, 2008
    #7
    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. Arne Vajhøj
    Replies:
    12
    Views:
    814
  2. Ian Collins
    Replies:
    0
    Views:
    576
    Ian Collins
    Apr 11, 2008
  3. Roedy Green
    Replies:
    14
    Views:
    1,212
    Mirek Fidler
    Apr 14, 2008
  4. Ian Collins
    Replies:
    2
    Views:
    551
    Matthias Buelow
    Apr 11, 2008
  5. Juha Nieminen
    Replies:
    10
    Views:
    586
    Mike Schilling
    Apr 13, 2008
Loading...

Share This Page