Re: Function pointers (Callback functions) in Java ?

From: HK (pifpafpuf_at_gmx.de)
Date: 02/28/05


Date: 28 Feb 2005 01:50:39 -0800

exquisitus wrote:
> correct me), I find that I have to wrap up the ADT which is so
naturally
> a struct, into a heavy, clumsy object. Everytime I receive data, I
have
> to create a new data object (an encapsulation of the C struct) and
then
> 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
receive
> 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
hanging
> 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.

  Harald.



Relevant Pages

  • Re: Looking for a more elegant way to do memory offsets
    ... The mallocfunction returns memory that is "suitably aligned" ... >which in it contains the pointer to the first element in the linked ... and here a "struct dem_mem" does not contain a pointer to another ... This is not a linked-list access, but rather an array access: ...
    (comp.lang.c)
  • Re: The Java no pointer big fat lie!
    ... Does Java have auto variables that construct and destruct by scope? ... Are object references a native memory pointer or an abstracted ... > and reference) ...
    (comp.lang.java.programmer)
  • Re: C Socket Programming question
    ... All I really need to make the socket connection is ... But over-writing random memory can be so much fun. ... > You still have to allocate memory and save the pointer to .h_addr. ... > for struct hostent *tmp as well as the space for its members ...
    (comp.unix.programmer)
  • Re: Objects
    ... i know that an object is a region of memory that can represent values. ... The struct becomes an object once it's defined. ... If you read my post you will find that I have described struct type ... after the pointer to which its address was assigned. ...
    (comp.lang.c)
  • Global Variable vs Struct Variable Question
    ... but it does not have pointer. ... sense that global variable has its own memory address individually. ... some local variables are stored in struct that struct always have ... Member functions inside struct or class can slow and degrade the ...
    (comp.lang.asm.x86)