This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

create 1000 or more objects in java efficiently  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do we create 1000 or more objects in java efficiently and what will be the memory overhead? And how can we get size of an object?
 
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross-posted at Stack Overflow and dream.in.code
 
Saloon Keeper
Posts: 5040
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1000 is a very small number. Don't worry about it.

As an aside, object creation used to be a costly operation a long time ago, but those days are long gone (15+ years).
 
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Cross-posted at Stack Overflow . . .

. . . where he got this reply:-

It's exactly the same as the total length of 1000 pieces of string. –

 
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:1000 is a very small number. Don't worry about it.

As an aside, object creation used to be a costly operation a long time ago, but those days are long gone (15+ years).


Probably the problem may arise if singular object's creation is very expensive?
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

khushi sharma wrote:. . .  And how can we get size of an object?

You can't. You can consider creating a List, adding 1,000,000 objects to it, and using the methods of the System class to get free memory, but that is about as near as you will get.
 
Tim Moores
Saloon Keeper
Posts: 5040
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Probably the problem may arise if singular object's creation is very expensive?


Then I would question the design that causes 1000 of them to be allocated.
 
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait for project Valhalla to be completed and turn the class into a value class.

OK, that's not 100% seriously, but when we do get it, we can have a lot of performance improvements if applied correctly. A quick example:
@ 40:22 (also check out the Loom example @ 19:10). Summarising the video: by adding one word (public class -> public value class) the example had a memory footprint of 4MB instead of 4GB (and a speed increase of 4s -> 290ms as well).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!