Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:48:06+00:00 2026-05-26T01:48:06+00:00

So I was messing around with some easy problems to get better at scala

  • 0

So I was messing around with some easy problems to get better at scala and I wrote the following program to calculate primes using an Eratosthenes’s sieve. When I bump up the number of primes to find, I noticed that my cpu would max out during the calculation. Now I have no clue why it’s using more than 1 core and I was afraid it would muck up the answer but it appears to be correct upon multiple runs so it must not be. I’m not using .par anywhere and most all of my logic is in for-comprehensions.

Edit: I’m using scala 2.9.1

object Main {
  val MAX_PRIME = 10000000

  def main(args: Array[String]) {
    println("Generating array")
    val primeChecks = scala.collection.mutable.ArrayBuffer.fill(MAX_PRIME + 1)(true)
    primeChecks(0) = false

    println("Finding primes")
    for (
      i ← 2 to MAX_PRIME if primeChecks(i);
      j ← i * 2 to MAX_PRIME by i
    ) primeChecks(j) = false

    println("Filtering primes")
    val primes = for { (status, num) ← primeChecks.zipWithIndex if status } yield num

    println("Found %d prime numbers!".format(primes.length))

    println("Saving the primes")
    val formatter = new java.util.Formatter("primes.txt", "UTF-8")
    try {
      for (prime ← primes)
        formatter.format("%d%n", prime.asInstanceOf[Object])
    }
    finally {
      try { formatter.close } catch { case _ ⇒ }
    }
  }
}

Edit 2: You can use the following snippet in a REPL to get the multi-threading behavior so therefore it has to be because of the for-comprehension (at least in scala 2.9.1).

val max = 10000000
val t = scala.collection.mutable.ArrayBuffer.fill(max + 1)(true)
for (
    i <- 2 to max if t(i);
    j <- i * 2 to max by i
) t(j) = false
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:48:06+00:00Added an answer on May 26, 2026 at 1:48 am

    It’s not your code that’s using multiple threads, it’s the JVM that is. What you are seeing is the GC kicking in. If I increase MAX_PRIME to 1000000000 and give it 6Gb of Java stack to play with I can see a steady-state of 100% of 1 CPU and about 4Gb mem. Every so often the GC kicks in and it then uses 2 CPUs. The following Java stack trace (pruned for clarity) show what’s running inside the JVM:

    "Attach Listener" daemon prio=3 tid=0x0000000000d13800 nid=0xf waiting on condition [0x0000000000000000]
    "Low Memory Detector" daemon prio=3 tid=0x0000000000a15000 nid=0xd runnable [0x0000000000000000]
    "C2 CompilerThread1" daemon prio=3 tid=0x0000000000a11800 nid=0xc waiting on condition [0x0000000000000000]
    "C2 CompilerThread0" daemon prio=3 tid=0x0000000000a0e800 nid=0xb waiting on condition [0x0000000000000000]
    "Signal Dispatcher" daemon prio=3 tid=0x0000000000a0d000 nid=0xa runnable [0x0000000000000000]
    "Finalizer" daemon prio=3 tid=0x00000000009e7000 nid=0x9 in Object.wait() [0xffffdd7fff6dd000]
    "Reference Handler" daemon prio=3 tid=0x00000000009e5800 nid=0x8 in Object.wait() [0xffffdd7fff7de000]
    "main" prio=3 tid=0x0000000000428800 nid=0x2 runnable [0xffffdd7fffa3d000]
       java.lang.Thread.State: RUNNABLE
        at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:76)
    "VM Thread" prio=3 tid=0x00000000009df800 nid=0x7 runnable 
    "GC task thread#0 (ParallelGC)" prio=3 tid=0x0000000000438800 nid=0x3 runnable 
    "GC task thread#1 (ParallelGC)" prio=3 tid=0x000000000043c000 nid=0x4 runnable 
    "GC task thread#2 (ParallelGC)" prio=3 tid=0x000000000043d800 nid=0x5 runnable 
    "GC task thread#3 (ParallelGC)" prio=3 tid=0x000000000043f800 nid=0x6 runnable 
    "VM Periodic Task Thread" prio=3 tid=0x0000000000a2f800 nid=0xe waiting on condition
    

    There’s only one thread (main) running Scala code, all the others are internal JVM ones. Note in particular there’s 4 GC threads in this case – that’s because I’m running this on a 4-way machine and by default the JVM will allocate 1 GC thread per core – the exact setup will depend on the particular mix of platform, JVM and command-line flags that are used.

    If you want to understand the details (It’s complicated!), the following links should get you started:

    • Java SE 6 Performance White Paper
    • Memory Management in the JavaHotSpot™ Virtual Machine
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm messing around with some windows functions using p/invoke. Occasionally, I get an error
I'm messing around with some C code using floats, and I'm getting 1.#INF00, -1.#IND00
Just got an Arduino and I'm messing around having some problems with the lights.
I am messing around with some jquery trying to get to grips with it.
I am currently messing around with some stuff for an idea for a site
The situation is that I've spent some time messing around with some experimental code.
I have finally started messing around with creating some apps that work with RESTful
I have been messing around with writing some stored procedures in .NET code with
Whilst messing around with some x86 asm, I got to wondering about cases where
I'm just messing around with some C++ at the moment trying to make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.