Re: Function pointers (Callback functions) in Java ?
From: HK (pifpafpuf_at_gmx.de)
Date: 28 Feb 2005 01:50:39 -0800
> correct me), I find that I have to wrap up the ADT which is so
> a struct, into a heavy, clumsy object. Everytime I receive data, I
> to create a new data object (an encapsulation of the C struct) and
> add it to my array. The perfomance hit on creating so many objects is
> (unsuprisingly) abysmal when compared to the C++ code. In practise, I
> can expect to receive about 50 such records per second, and can
> as much as 50,000 records in total. The thought of spawning 50 new
> object a second - not to mention having 50,000 bloated objects
> around in memory is not a pleasing prospect.
Lets compare a C struct to a Java object. The memory
occupied by the fields is pretty much the same.
The Java object typically has one additional field, a pointer
to the Class representation to which the object belongs.
Looking at an array of those things, Java needs
another pointer to point to the object, because
objects themselves are not put into the array, only
ever the pointers to them.
>>From the memory perspective this is an overhead of
8 bytes and it depends of course on your setting
whether this is tolerable or bloated.
Now for the performance: In C, malloc and free are
not exactly cheap operations as you will see when
profiling software which does a lot of them.
In Java, free (i.e. garbage collection) is probably
a bit more expensive than what C free does. On the
other hand, a `new' is less expensive than malloc
because garbage collection arranges free blocks
of memory much more neatly than free does.
The bottom line is in both situations: don't
request and abandon memory unnecessarily. In C++
you work with a memory pool. In Java you can do
the same. There are implementations of it, but
depending on your setting, you just preallocate
a lot of the objects you need and when it
comes to receiving the data, just fill them.