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 7010607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:00:49+00:00 2026-05-27T22:00:49+00:00

I am using a system which needs to initialize many objects using transactions, and

  • 0

I am using a system which needs to initialize many objects using transactions, and for reasons beyond the scope of this question these transactions must be passed into the constructors. Like this:

trait Mutable

class Txn(i: Int) {
  def newID(implicit m: Mutable): Int = i
  override def finalize(): Unit = println("Finalised " + i)
}

class User(t0: Txn) extends Mutable {
  val id = t0.newID(this)
}

Now I am fearing there is a problem with garbage collecting the transactions:

val u = new User(new Txn(1234))
System.gc()  // hmmm, nothing seems to happen?

So my question is: Does the t0 constructor argument ever get garbage collected, or do I create a memory leak here? In an equivalent Java code, I guess I’d have something like this:

public class User implements Mutable {
    final int id;
    public User(Txn t0) {
        id = t0.newID(this);
    }
}

and I am sure t0 is collected. But is this true in the Scala case?

If not, how can I ensure t0 is garbage collected? Remember that I must pass in the transaction as a constructor argument, because the User class implements some traits which must be passed into Txn‘s methods, thus those methods (like newID) cannot be called before constructing User.

I have tried before to construct everything that uses the transaction outside of the user object, with tons of lazy interdependent vals, but that was really messy. For example, this, which is already halfway unreadable, produces a stack overflow:

trait User extends Mutable { def id: Int }

def newUser(implicit tx: Txn): User = {
  lazy val _id: Int = tx.newID(u)
  lazy val u  = new User { val id: Int = _id } // oops, should be lazy val id!
  u
}

val u = newUser(new Txn(1234))

You can imagine that it really sucks that the compiler won’t spot the problem with the missing lazy val here, so I would definitely prefer the constructor arg variant.

  • 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-27T22:00:50+00:00Added an answer on May 27, 2026 at 10:00 pm

    If absolutely necessary, I recommend you use javap to see what the class has compiled into. Some rules to avoid getting the constructor argument turned into a class parameter:

    • Don’t use it on def or lazy val.
    • Don’t use it on assignment that do pattern matches (like val (a, b) = f(x)).
    • And, of course, do not declare it as val or var.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a positioning system which using matrixes can not satisfy my needs.
I am using a System.Random object which is instantiated with a fixed seed all
I have a system which is writing files to a folder using FTP. I
I have a system which contains multiple applications connected together using JMS and Spring
I'm using a data transmission system which uses a fixed SYNC word (0xD21DB8) at
We are using nHibernate 1.2 in a system which is not performing good. Will
I have started using Linq to SQL in a (bit DDD like) system which
I get security exception when using System.Net.WebClient to do HTTP requests, which is due
Using any tools which you would expect to find on a nix system (in
I've built app using wmp.dll which is Windows System File in my XP machine.

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.