Re: C++ vs Java "new" (no flame war please!)
- From: mlw <mlw@xxxxxxxxxxxxxx>
- Date: Sun, 01 Apr 2007 16:33:16 -0400
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.
.
- Follow-Ups:
- References:
- C++ vs Java "new" (no flame war please!)
- From: mlw
- Re: C++ vs Java "new" (no flame war please!)
- From: Lew
- C++ vs Java "new" (no flame war please!)
- Prev by Date: Re: C++ vs Java "new" (no flame war please!)
- Next by Date: Re: C++ vs Java "new" (no flame war please!)
- Previous by thread: Re: C++ vs Java "new" (no flame war please!)
- Next by thread: Re: C++ vs Java "new" (no flame war please!)
- Index(es):
Relevant Pages
|
|