Recently, I've written some Android app in scala, working with Eclipse. Everything was okay until my program had only several classes / activities. When I added more code, JVM started to crash, eclipse often freezed and adb constantly lost connection. Task manager showed that Java process was taking from 800 up to 1300 MB memory!
My computer's hardware parameters are quite nice like i7 3rd gen, 8GB DDR3 and SSD 256. So it's not the reason.
Also my software is up to date and consits of the newest version of Eclipse, Android SDK, Scala and Java development kit, all plugins for eclipse. -vmargs are configured for scala development but it didn't help at all. So I eliminated it to as a cause of problem.
When I rewrite my code to Java everythig goes excellent and I can go on with my project.
Has anyone find already some solution for working with scala in Android project? I read a couple of similar topics here and on google groups but there was no answer on this question.
EDIT:
I profiled my code with visual vm, but heap space usage was normal. Here's some snippet of my code.
imports ...
class MainActivity extends FragmentActivity {
private lazy val timePicker = findViewById(R.id.timePicker).asInstanceOf[TimePicker]
private lazy val spinner_sourceBank = findViewById(R.id.spinnerSource).asInstanceOf[Spinner]
private lazy val spinner_targetBank = findViewById(R.id.spinnerDestination).asInstanceOf[Spinner]
private lazy val myApplication = getApplication.asInstanceOf[MyApplication]
private var ignoreTimeChange = false
override protected def onCreate(savedInstanceState: Bundle) = {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
timePicker.setIs24HourView(true)
initializeSpinner(spinner_sourceBank)
initializeSpinner(spinner_targetBank)
}
private def initializeSpinner(spinner: Spinner) = {
val adapter = ArrayAdapter.createFromResource(this, R.array.banks, R.layout.spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.setAdapter(adapter)
}
def reset(w: View) = {
timePicker.setCurrentHour(13)
timePicker.setCurrentMinute(00)
spinner_sourceBank.setSelection(0)
spinner_targetBank.setSelection(0)
}
def submit(w: View) = {
if (spinner_sourceBank.getSelectedItemPosition == 0 || spinner_targetBank.getSelectedItemPosition == 0) {
new AlertDialog.Builder(this).setPositiveButton("OK", new OnClickListener {
override def onClick(dialog: DialogInterface, which: Int) { dialog dismiss }
}).setMessage(getString(R.string.alert_title)).setTitle("Alert Dialog").setIcon(android.R.drawable.ic_dialog_alert).create.show
} else manageFragments(sourceBank, targetBank)
}
def manageFragments(sourceBank: String, targetBank: String) = {
val fragmentTransaction = getSupportFragmentManager.beginTransaction
if (targetBank == sourceBank) myApplication.setCurrentFragment(MyApplication.NOW)
else myApplication.setCurrentFragment(MyApplication.FIRST_SESSION)
fragmentTransaction.replace(R.id.fragment_container, myApplication.getCurrentFragment)
fragmentTransaction.commit
}
override def onCreateOptionsMenu(menu: Menu): Boolean = { getMenuInflater.inflate(R.menu.activity_main, menu); true }
}
I could test my application on mobile device and it ran pretty well. I also could write my code, but eclipse behaved like ran on old computer - switching tabs took a lot of time and so building and deploying.
Add these to eclipse.ini (or edit if present), as are more optimised for Scala development: They should appear below eclipse entries, such as -vm:
-Xverify:none
-Dosgi.requiredJavaVersion=1.6
-Xms900m
-Xmx900m
-Xss2m
-XX:PermSize=256m
-XX:MaxPermSize=256m
-XX:MaxGCPauseMillis=20
-XX:MaxHeapFreeRatio=70
-XX:+ScavengeBeforeFullGC
-XX:+UseParallelOldGC
-XX:+UnlockExperimentalVMOptions
-XX:+UseFastAccessorMethods
-XX:ReservedCodeCacheSize=128m
-XX:+TieredCompilation
-XX:+AggressiveOpts
Probably unlikely, but if you are using CMS GC, remove references to it from the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With