views:

918

answers:

3

Which Java blocking queue is best for multiple producer and single or multiple consumers scenarios ?

I am testing with LinkedBlockingQueue but i am getting OutOfMemoryError exception.

I am trying to achieve following things.

  1. producer creates a object & put in a queue.
  2. consumer grabs the data from queue & insert into DB. There would be 400 producers and i can adjust consumers as i wish.

let me know any idea. Thanks,

Update 10/07,

Thanks for your input gents,

Producer : It should listen to Server Socket. It reads the data from socket & construct the object (Domain objects) & put in a queue. Consumer : Take the object from queue & insert into DB (Hiberante & connection pooling supported) This my real environment.Process should be able to process at least 200 records/sec.I am testing the scalability of process and how to improve that. I hope, This will give better idea.

Helpful links : link vmoptions link Monitoring and Managing Java SE 6 Platform Applications link BlockingQueue

Thanks.

+1  A: 

Is each producer a separate thread ? Don't forget that each thread will allocate (by default) 512k of memory for its stack (in your case requiring 200Mb of VM memory just for the threads). You can reduce this via -Xss.

Alternatively, just how big are the objects you're queuing ? I don't think you have a queue problem so much as some sort of scaling issue - e.g. producers producing faster than consumers can consume.

Brian Agnew
Thillakan
If your domain object is (say) 500 bytes in size then your queue will consume .5kb * 90e3 = 45Mb. Which isn't that much, really. So I would look again at your VM memory, and perhaps a) increase the VM size, b) decrease the thread stack size. The above is informed guesswork, btw, without knowing how big your domain object is, or how much else your app is doing.
Brian Agnew
Thanks for your interest. I increased the VM Size to -vmargs as -Xms512m -Xmx1024m. and i configured sleep time 1000ms for producers(400) and 100ms for consumers(10). Now process could be able to handle 400 record/sec
Thillakan
+1  A: 

It seems that LinkedBlockingQueue is the best choice for your problem. I would suggest you to run your program with the switch -XX:+HeapDumpOnOutOfMemoryError and analyze the dump file to see what has caused the problem. Then you'll be able to solve it much easier.

Boris Pavlović
Thanks, I could not get the dumping file i set the -XX:+HeapDumpOnOutOfMemoryError to program argument , REF link [http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/clopts.html]
Thillakan
Try searching the disk for *.hprof files
Boris Pavlović
Yep,I did not properly pass the JVM option. Now i got the file but when i try to eecute with jhat utility it is throwing exception.
Thillakan
jhat -versionjhat version 2.0 (java version 1.6.0_11)
Thillakan
jhat java_pid8296.hprofReading from java_pid8296.hprof...Dump file created Tue Aug 11 13:27:50 EST 2009Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Thillakan
at com.sun.tools.hat.internal.parser.HprofReader.getStackTraceFromSerial(HprofReader.java:632) at com.sun.tools.hat.internal.parser.HprofReader.readInstance(HprofReader.java:722) at com.sun.tools.hat.internal.parser.HprofReader.readHeapDump(HprofReader.java:474) at com.sun.tools.hat.internal.parser.HprofReader.read(HprofReader.java:226) at com.sun.tools.hat.internal.parser.Reader.readFile(Reader.java:79) at com.sun.tools.hat.Main.main(Main.java:143)
Thillakan
it is throwing OutOfMemoryError. Do u have any idea ?
Thillakan
Ye ye I got it. jhat -J-mx512m java_pid8296.hprof Thanks you very much
Thillakan
i've just wanted to tell you to increase the memory for jhat tool :)
Boris Pavlović
+5  A: 

The problem is not which Queue implementation you use but rather how to approach the problem of throttling your producers if your consumers cannot keep up. One possible solution is to create a LinkedBlockingQueue with a fixed capacity, and have your producers call offer(E e), which will return false if the queue is full.

Another possible solution is to tailor the number of producers and consumers accordiingly.

Adamski
+1 I agree it seems that your queue doesn't have a sensible capacity - the default is Integer.MAX_VALUE which is rather large. If you have set the capacity to something sensible and still have problems then check your Java heapsize settings -Xmx etc...
pjp
Thanks guys, I can not reduce the producers size in real time so i have to handle it. If i use the offer I lose that object is n't it ? I am using put () and take(). which will wait depending on queue state. I configured -vmargs as -Xms512m -Xmx1024m in eclipse run configuration.
Thillakan