新手OutOfMemory问题

时间:2010-04-30 01:26:58

标签: scala actor out-of-memory

我正在尝试创建一个生产者/消费者类型Scala应用程序。 LoopControl只是不断向MessageReceiver发送消息。 MessageReceiver然后将工作委托给MessageCreatorActor(其工作是检查地图的对象,如果没有找到则创建一个并启动它)。 此MessageActor创建的每个MessageCreatorActor都与ID相关联。 最终,这是我想要做业务逻辑的地方。 但是15分钟后我的内存耗尽了。 任何帮助表示赞赏

import scala.actors.Actor
import java.util.HashMap;
import scala.actors.Actor._

case object LoopControl
case object MessageReceiver
case object MessageActor
case object MessageActorCreator

class MessageReceiver(msg: String) extends Actor {

 var messageActorMap = new HashMap[String, MessageActor]
 val messageCreatorActor  = new MessageActorCreator(null, null)

 def act() {
  messageCreatorActor.start
  loop {
   react {
   case MessageActor(messageId) =>   
   if (msg.length() > 0) {         
     var messageActor = messageActorMap.get(messageId);

     if(messageActor == null) {
      messageCreatorActor ! MessageActorCreator(messageId, messageActorMap)
     }else {   
      messageActor ! MessageActor
     }
    }
   }
  }
 }
}

case class MessageActorCreator(msg:String, messageActorMap: HashMap[String, MessageActor]) extends Actor {
 def act() {
   loop {
     react {
       case MessageActorCreator(messageId, messageActorMap) =>
         if(messageId != null ) {
    var messageActor = new MessageActor(messageId);
    messageActorMap.put(messageId, messageActor)
    println(messageActorMap)
    messageActor.start
    messageActor ! MessageActor
        }
     }
   }
    }
}

class LoopControl(messageReceiver:MessageReceiver) extends Actor {
  var count : Int = 0;
  def act() {
    while (true) {
          messageReceiver ! MessageActor ("00-122-0X95-FEC0" + count)
          //Thread.sleep(100)
          count = count +1;
          if(count > 5) {
           count = 0;
          }
    }
  }
}

case class MessageActor(msg: String) extends Actor {
 def act() {
   loop {
     react {
       case MessageActor =>
         println()
         println("MessageActor: Got something-> " + msg) 
     }
   }
    }
}

object messages extends Application {

 val messageReceiver = new MessageReceiver("bootstrap")
 val loopControl = new LoopControl(messageReceiver)

 messageReceiver.start
 loopControl.start
}

1 个答案:

答案 0 :(得分:2)

我想知道您的代码是否实际上是在地图中找到现有对象?

如果找不到旧版本,它将继续创建新的MessageActors,直到内存不足为止。

尝试测试。