• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple threads using same Java Bean

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When multiple instances (i.e. multiple users) of the same java bean are running in the same JVM, does the JVM manage multiple complete copies of the Bean, or is it smart enough to have ONE copy of methods, with separate variables, managed for each thread?

I'm referring to a plain java bean here, not an EJB.

We have a group of very large java beans that perform a server-side domain business function. We are performance testing multiple users of this group using the JMeter scripting tool. We encounter out of memory errors as we 'ramp up' the number of concurrent users, so I'm trying to learn more about how the JVM manages memory relative to this scenario.

Thanks
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are your threads accessing the same instance of the bean or multiple instances. Java will create seperate copies of data members for each instance of the bean.

Using a seperate instance of the bean will result in the memory usage going up, but if your cleanup code is correct, the beans should get garbage collected

Using the same instance of the bean might reduce memory usage, but you have to be careful about synchronization issues. In a nutshell, if 2 threads are modifying the same data member, then you need to make sure only one thread can modify the data at a time, otherwise the contencts of the member will be nknown. You do that by placing code in synchronized blocks or marking your functions as synchronized. Too much synchronization does reduce the performance of an app. Incorrect synchronization can create deadlocks(I'm not 100% sure on that though. I don't know if Java has an inbuilt solution for resolving deadlocks) Most web apps that I have seen try to create new beans for every request (unless the design/requirements need some data shared across requests/users). Although, this uses more memory while the request is being processed, it is much better than over-synchronizing your code
 
Randy Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our case, we are using a separate Bean instance for each thread.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to what Jayesh wrote, the byte code of the class, containing the definition of the methods, will only be loaded once, regardless of the number of instances you have.
 
Randy Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for this response. Can you direct me to a document/book which discusss the mechanics of this process in more detail?
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Randy Johnson:
In our case, we are using a separate Bean instance for each thread.



Then each concurrent user will use memory. Ideally, all beans allocated in each run of the thread should get cleaned up. However, you need to check for memory leaks. If some beans are not getting garbage collected then your memory usage will keep creeping up intil you run out of memory. If you are sure about having no memory leaks then you may want to think about how you can reduce the meory footprint of each user, or a better way to go is to make you application more scalable;ie; design it so you can host your application in multiple servers

Regarding lifecycle of a Java object, here is an article that talks about Garbage collection and how objects are stored in memory. I think you might get better information if you look at books on Java Performance. I don't know any specific books, but this question has been brought up before. I think you might try searching around this message board for book recommendations
 
reply
    Bookmark Topic Watch Topic
  • New Topic