Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to create large number of objects in java so the the memory overhead is minimum.

What I have tried:

i have tried for loop for creating objects but i want to know the memory overhead associated with the concept of creating objects in java
Posted
Updated 5-Dec-18 0:26am

It depends on what type of container you are using, a simple Java array, a List, or any other custom developed container. Each of these containers either have a zero, constant, or somewhat variable overhead to maintain other operations. Like a List type might store some extra memory space to provide a faster insertion, whereas arrays take up O(N) space...
Quote:
i want to create large number of objects in java so the the memory overhead is minimum
I am not sure I follow you on this or not, but each object is going to take its own space in the memory, either created via loop, function calls, or internally.

Objects in Java are references to their instances, creating an array of 1000 objects does not mean you create 1000 objects, you would have to new them at some time later. That is how Java's type system works.
Quote:
memory overhead associated with the concept of creating objects in java
How Java handles that underlying inside the JVM, that much I am not sure but it does have a function call that creates the object itself, and any internal objects or structures. It is mostly a recursive call, any overhead associated with them is basically because of internal creation of objects, and getting the references of the variables and their objects—JVM realm.

Last but not least, do not bother with the internals of Java unless you have to write a compiler, otherwise, just do your best at a high-level. See here, Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^]
 
Share this answer
 
v2
Quote:
i want to create large number of objects in java so the the memory overhead is minimum.
There is no 'memory overhead', the memory allocated it the one necessary to create the objects. If you want to use less memory then reduce, if you can, the memory required by each object to its bare minimum (for instance, removing redundant info).

As an alternative, completely different and interesting approach, you might have a look at the Flyweight pattern - Wikipedia[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900