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

Discussion in 'Java' started by Ian Collins, Apr 11, 2008.

  1. Ian Collins

    Ian Collins Guest

    Razii wrote:
    > On Thu, 10 Apr 2008 19:14:12 -0500, Razii
    > <> wrote:
    >
    >>> However, part of my C++ programming style just naturally also avoids
    >>> doing tons of news and deletes in tight loops (which is, again, very
    >>> different from eg. Java programming where you basically have no choice)

    >
    > Let's test this about the keyword "new" and tight loops. Because in
    > most cases Java allocates new memory blocks on it's internal heap and
    > bypasses memory allocation mechanisms of the underlying OS, the
    > keyword "new" doesn't mean the same thing that it does in C++, where
    > each "new" allocation request is sent to the operating system, which
    > is very slow.
    >
    > Creating 10000000 new objects with the keyword 'new' in tight loop.
    >

    If a C++ programmer had to do this in the most efficient way possible,
    he/she would use a custom allocator.

    > 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;
    > }


    Leaks 10000000 objects.

    >
    > for (int i=0; i<=10000000; i++) {
    > Test test = new Test(i);
    > if (i % 5000000 == 0)
    > System.out.println (test);
    > }


    Does the Java allocator/GC combination recycle the objects in the loop?

    --
    Ian Collins.
     
    Ian Collins, Apr 11, 2008
    #1
    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:
    810
  2. Roedy Green
    Replies:
    14
    Views:
    1,208
    Mirek Fidler
    Apr 14, 2008
  3. asterisc
    Replies:
    6
    Views:
    499
    Mark Space
    Apr 12, 2008
  4. Ian Collins
    Replies:
    2
    Views:
    547
    Matthias Buelow
    Apr 11, 2008
  5. Juha Nieminen
    Replies:
    10
    Views:
    583
    Mike Schilling
    Apr 13, 2008
Loading...

Share This Page