Re: C++ vs Java "new" (no flame war please!)



Lew wrote:

mlw wrote:
> (snipped description of custom 'new' operator)

Is there a way to create 10 to 100 million objects in Java with a
reasonable system configuration?

Sure, given the same considerations of available heap that you would have
in the C++ world.

Give or take, I guess.


Let's assume you want to create N objects where N is the largest number of
such objects that would fit in available memory.

List <Foo> stuff = new ArrayList <Foo> (N);

That's one memory alloc, sure.

for ( int x = 0; x < N; ++x )
{
stuff.add( new Foo( getAString() ) );
}

The above code is exactly my problem with Java. In C++ I can overload new
and put all the objects anywhere I want, even in to one contiguous memory
block calling malloc merely once and combining the object and the string in
one allocation, for instance:

(Remember this is a simplification for example purposes, but the technique
is the important thing.)

unsigned char * myshared_block = shared_alloc(MAX_SIZE)
size_t curr_offset=0;

void *foo::operator new(size_t size, char * str)
{
size_t cbstr = strlen(str)+1;
size_t cb = size + cbstr;

foo * fooT = (foo *) &myshared_block[curr_offset];
curr_offset += cb;
char *pstr = (char *)&foo[1];
strcpy(pstr, str);
fooT->str = pstr;
return (void *) fooT;
}

In the above code, I can pre-allocate a single memory block and pull objects
out of it until it is empty.

Every memory allocation has overhead, in GCC malloc, it is probably 4 bytes,
8 bytes on 64 bit systems. So, if you have fairly small objects, and lots
of them, a good chunk of memory will be eaten up with malloc overhead. If
you have a small object with a string, you will have two memory
allocations!

Obviously this is a rare problem, but it is a problem none the less.



doSomethingWith( stuff );


If you don't need them all in memory at once, it's even easier:

for ( int x = 0; x < N; ++x )
{
Foo foo = new Foo( getAString() );
doSomethingWith( foo );
}

I posit the 'getAString()' method as where you'd obtain the equivalent of
the 'char * string' in the C++ example. I assumed you'd use a different
string for each instance of Foo.


.



Relevant Pages

  • Re: greetings
    ... a string that will be the same object in memory each time you use it. ... but:foo and:foo will refer to the same object. ... database connection parameters are good example for variable length arguments. ...
    (comp.lang.ruby)
  • Re: Virtual function and multiple inheritance
    ... __vfptr for Foo contains Derived overridden virtual methods for Foo, ... itself's virtual methods implementation, and __vfptr for Goo contains Derived ... Here pF is a pointer to an object with the same memory layout as a Foo ...
    (microsoft.public.vc.language)
  • malloc, mmap, and unexec issue
    ... I have a problem regarding mallocusing mmap() for large memory ... allocations. ... Arguments can be passed to 'foo' to change ...
    (comp.os.linux.development.system)
  • Re: Overwriting a portion of a binary file
    ... the whole memory might not be enough. ... > my code always insists on storing 'bar' after 'foo'. ... We don't want a peek, we want a long, lingering look at the whole thing. ...
    (comp.lang.c)
  • File is a special class?
    ... In order to make it's use as transparent as possible, i overloaded the 'require' method to search as well in the memory and this works fine at the moment. ... class Foo ... Creating a new Bar ... Should be type Foo and is: ...
    (comp.lang.ruby)