2

I have entity objects with large no of uninitialized fields that are of datatype ranging from primitives to collection objects, etc. However the issue is that for most of the time not all the fields are required so they are left uninitialized.

A large no of such entity objects are created & destroyed upon each user request! So I am a bit worried about if this might be a concern in terms of memory or performance ?! The fields are not initialized, unless they are required. So I wanted to ask whether it really matters if I declare a lot of fields within my class but initialize/use just a few ones?

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

1 Answers1

1

As by this java tutorial page Fields that are declared but not initialized will be set to a reasonable default by the compiler. That means that each of your uninitialized fields would take few bytes, typically 4 bytes for ints and objects.

That said, it depends greatly on your specific system - how much fields your objects have, how many objects are created per request, how many requests per second, etc. That could have different effects on different garbage collectors.

I suggest you do some profiling - see the load on the system (CPU, memory) with the current object and a lot of requests. Then, create some fake objects with less fields and re-run your test with the same load. If you can't measure any difference, then there is probably nothing to worry about, and you can go with few extra unused fields.

jmruc
  • 5,714
  • 3
  • 22
  • 41