Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Call Center Queue

CallCenter Challenge: OutOfMemory Error

I think I'm following the instructions, and apparently the code works for the first call to acceptCustomer by the evaluating code, but fails in the playHoldMusic() step in the next call --assuming the output is reflective of how far we have gone. (Actually, I never see any output from playHoldMusic(), so I'm not sure how we could be running out of memory there.)

Any ideas? Here's the content of output.html:

Hello Bob, my name is Canary, how can I assist you. ... Is there anything else I can help you with? Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3236) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153) at java.io.PrintStream.write(PrintStream.java:480) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104) at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185) at java.io.PrintStream.write(PrintStream.java:527) at java.io.PrintStream.print(PrintStream.java:669) at java.io.PrintStream.println(PrintStream.java:806) at CallCenter.playHoldMusic(CallCenter.java:44) at CallCenter.acceptCustomer(CallCenter.java:22) at JavaTester.run(JavaTester.java:110) at JavaTester.main(JavaTester.java:41)

import java.util.ArrayDeque;
import java.util.Queue;

public class CallCenter {
  Queue<CustomerSupportRep> mSupportReps;

  public CallCenter(Queue<CustomerSupportRep> queue) {
    mSupportReps = new ArrayDeque(queue);

  public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
     * TODO (1) 
     * Wait until there is an available rep in the queue.
     * While there is not one available, playHoldMusic
     * HINT: That while assignmentcheck loop syntax we used to 
      *      read files seems pretty similar
    while((csr = mSupportReps.poll()) == null) { 

     * TODO (2) 
     * After we have assigned the rep, call the 
     * assist method and pass in the customer

     * TODO (3) 
     * Since the customer support rep is done with
     * assisting, put them back into the queue.


  public void playHoldMusic() {
    System.out.println("Smooooooth Operator.....");

import java.util.List;
import java.util.ArrayList;

public class CustomerSupportRep {
  private String mName;
  private List<Customer> mAssistedCustomers;

  public CustomerSupportRep(String name) {
    mName = name;
    mAssistedCustomers = new ArrayList<Customer>();

  public void assist(Customer customer) {
    System.out.printf("Hello %s, my name is %s, how can I assist you.%n",
    System.out.println("Is there anything else I can help you with?");

  public List<Customer> getAssistedCustomers() {
    return mAssistedCustomers;

public class Customer {
  private String mName;

  public Customer(String name) {
    mName = name;

  public String getName() {
    return mName;


1 Answer

The answer, it seems, is that the loop happens outside the code we are to modify. Use an if instead of a while, and return after starting the music when there is no available CSR, rather than continuing to loop expecting some other thread to add a CSR to the mSupportReps queue while you loop.