When
java was developed first there was no real way to measure the size of an object. Java does not provide any sizeOf() operator like C. This is because in C programmers have to manually allocate memory using malloc().In java, memory allocation and object construction are both tied together, This makes it more difficult to find the size of an object.
Traditional method used is to measure the memory used by the program before creating the object and after creating the object and then checking the difference. The difference will give us the size of memory allocated for that object unless -
(1) The object is created first time, resulting of the loading of the Class for that object in memory which will be in many cases much bigger than the object itself
(2) Garbage collector kicked in after we measured the used memory before creating the object but before measuring the memory used after creating the object. In this case the size recorded could be negative.
According to my knowledge- Yes you can specify the maximum size of an object in memory. maximum size of the object is constructed by its instance variables.