This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

create 1000 or more objects in java efficiently  RSS feed

 
Greenhorn
Posts: 12
  • 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: 2329
293
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: 5219
143
  • 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: 62803
203
  • 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: 6492
441
BSD Linux Mac OS X VI Editor
  • 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: 62803
203
  • 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: 5219
143
  • 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: 21601
101
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).
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!