Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton or Prototype for DI bean injection?

 
Dinesh Sundrani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think singleton scope is a good idea in multithread environments, how do we maintain the synchronism if multiple threads are trying to modify the same single bean object?

Do you think using prototype scope is better in such a case or does having a single copy of the bean for each client makes it a dead tortoise speedwise?

I am especially interested in high throughput, ultra low latency trading applications where timed execution is of prime essence. Losing a minute (in creating a copy of the bean), could cause the trade to stale out and enter a dead queue, eventuall losing out on $$.

Regards,
DS [SCJP 1.5, SCWCD, SCDJWS]
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is no instance variables, the bean is thread-safe.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object instantiation in Java is *very* fast. Unless it has very lengthy startup/initialization requirements it's unlikely instantiation would be the cause of any major issues.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create too much objects can cause issues because each object uses memory.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Too many" is pretty vague: typical Java applications create and destroy tens, if not hundreds, of thousands of objects during normal execution.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every single object uses memory. Amount of memory depends on number of objects multiply concurrent users.
If using some feature of application requires creating 10 objects, if there are 100 concurrent users, there will be 1000 objects created at a given time. But if the objects are singleton there will be only 10 objects regardless the number of concurrent users.

My recommendation is make services stateless if possible and use singleton scope.

Creating too many objects can cause full GC which is very bad for applications that response time is important, and the worst is OutOfMemoryError happens.

I don't think time used in creating objects will take up to a minute (even consider concurrent users), but if combine with full GC/repeately full GC, it's possible.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if there are 100 concurrent users, there will be 1000 objects created at a given time

Well yeah; I understand math. My point was simply that object instantiation time is almost never an issue unless there's time-consuming initialization.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic