Profile cover photo
Profile photo
Mojtaba Saberi
Keep calm and write code :)
Keep calm and write code :)

Communities and Collections
View all

Post has shared content
Wonderful picture from +Abbie Stewart
"Thunderstorm over an Abandoned Barn, Saskatchewan Prairie, Canada."
- Photo by Mark Duffy
Add a comment...

Post has shared content
Solve App Problems 10x Faster with AppDynamics – Monitor production apps at code-level depth with minimal overhead. Start a FREE Trial!
Memory, Memory, Memory…
Memory is a critical part of Java, in particular, the management of memory. As a developer, memory management is not something you want to be doing on a regular basis, nor is it something you want to do manually. One of the great benefits of Java is its ability to take care of the memory model for you. When objects aren’t in use, Java helps you out by doing the clean up.
But that’s also where the problems begin. With Java, maybe your app isn’t using an object anymore, but if you don’t tell the virtual machine that you’re not using it anymore it doesn’t clean it up. This is a memory leak. We’ve all seen them. Objects start building up in your heap and your app comes grinding to a halt.
Memory leaks typically happen as a result of improper programming – usually its a case where the developer didn’t relieve all references to an object. Similar objects in Java, as you know, are put together into collections or maps, so the problem can get compounded if you don’t remove particular data sets from collection. The more that goes into collection, the more space you lose.
When Java manages the memory model for you, or rather the creation/destroying of objects that are unused, it puts them in a heap. This heap is always of a certain size and has a maximum of available space. If memory management is done poorly, you will run out of that space for the heap. Collections add up and then the JVM crashes.
Diagnosing Leaks
Traditionally, there have been two primary methods for diagnosing memory leaks – heap dumps and profilers.
The first, a heap dump, basically allows you to see which object is holding a reference to the collections. It gives you a good idea of what objects are causing the problem, but it doesn’t tell you who is accessing the collection and who’s not. It tells you where the collection is, but not the characteristics of who’s using it. Heap dumps are also usually quite large, in gigabytes, and a big heap dump is tedious to review. It takes a lot of resources to analyze and open a heap dump, then read it and identify the issue.
The second method, a combination of a heap dump and a profiler, gets you a little bit closer, but not much. Memory profilers try to help you analyze your heap dump. They have live data and now you know who is creating the objects, but you still don’t what’s actually causing the leak.
Let’s say I have an employee object. The employee object gets put in collection and the profiler will tell you who created it. What the profiler doesn’t tell you is who put it in and who removes it from the collection. Profilers tell you the birth of the object, rather than cause of the leak. By all means, this helps you narrow it down, but you need solid knowledge of the application for a profiler to really help you identify the cause, and then it still takes significant time and resources to find your leak.
We hear this from a lot of people. Many companies try these tools, but every couple of days their app crashes. So what do they do? They restart the JVM or CLR. Their apps crash again, and again, they restart. The application management is a nightmare because they have no way to find or fix their memory leaks.
Both heap dumps and profilers can be helpful in development and pre-production, but once your apps are out in the wild, profilers just aren’t useable. Profilers introduce a huge amount of overhead and heap dumps pretty much stop all processing for apps in production. You basically need to take the application on that JVM/CLR offline in able to get anything done.
These tedious processes are getting harder and harder to maintain as today’s applications advance. Heaps are getting bigger as applications get more complex, and eventually, these methods just don’t cut it.
AppDynamics & Memory Leaks
Our approach to date has been to provide comprehensive transaction snapshots, particularly obvious code problems that can give you some insight into where the issue might be. To effectively isolate and address memory leaks, transaction and code path analysis is critical.
That brings us to some interesting developments from AppDynamics. We’ve given companies a way to directly identify the root cause of their memory leak. You can automatically detect the leak, identify who’s creating it, as well as which code path or business transaction is causing it. You can read more about it here.
The benefit of this? Less downtime and reduced MTTR. We’re pretty excited. Hope you are, too.
Solve App Problems 10x Faster with AppDynamics – Monitor production apps at code-level depth with minimal overhead. Start a FREE Trial!
Add a comment...

Post has shared content
Scala can be used to build Android applications, as an alternative to Java or Kotlin. Unlike them, setting up an Android project in Scala with SBT is not straightforward, and can give us some headaches to get it right. To show how this can be done, we are going to create new project template using the Android SDK Plugin for SBT.
Required tools
In order to develop Android apps in Scala, you need a minimum set of tools: SBT and Android SDK.
Install SBT
You can install SBT on Mac OSX using Homebrew.
$ brew install sbt
To install SBT on other operating systems, you can follow the instructions on the official documentation.
Install the Android SDK
You can just download the latest version of the Android SDK from the Developer website and follow the installation instructions. Alternatively, you can install Android Studio, which comes with the Android SDK and emulators.
Set the ANDROID_HOME environment variable
On Mac OSX/Linux, you can just export the variable
$ export ANDROID_HOME=path_to_your_android_sdk
or add it to your bash_rc or bash_profile.
NOTE: On Mac OSX, if Android Studio is installed, the Android SDK is usually located at/Users/your_user_name/Library/Android/sdk/
Add SBT plugin
The easiest way to install the Android SDK Plugin for SBT is to do it globally. For this, you’ll need to create a file in the SBT plugins folder:
and add the following line:
addSbtPlugin("org.scala-android" % "sbt-android" % "1.7.0")
Create the Android project
Let’s create a folder for our Android project:
$ mkdir my-project
$ cd my-project
Now, we are going to use the SBT plugin to create a template project. First, run SBT:
$ sbt
Then, use the plugin to create the project.
> gen-android
For example:
> gen-android com.codurance scala_on_android
The project structure created looks like this:
|-- project/
| |-- android.sbt
| |--
|-- src/
| |-- androidTest/
| |-- java/
| |-- com/
| |-- codurance/
| |--
| |--
| |-- main/
| |-- res/
| // Android resorces folders
| |-- scala/
| |-- com/
| |-- codurance/
| |-- MainActivity.scala
| |-- AndroidManifest.xml
|-- build.sbt
|-- lint.xml
The SBT plugin creates a project structure with the minimum files needed to run an Android project, plus a setup for running instrumentation tests. Notice that the test classes generated are in Java, and the MainActivity is in Scala.
The most interesting file is build.sbt. I’ve added some comments to explain what’s the purpose of each line.
// Version of the Scala runtime
scalaVersion := "2.11.8"

// Use the Android plugin
// Add support for vector drawables

// Android version code (Same as versionCode on Gradle projects)
versionCode := Some(1)
// Android version name (Same as versionName on Gradle projects)
version := "0.1-SNAPSHOT"

// Instrumentation tests runner (Same as testInstrumentationRunner on Gradle projects)
instrumentTestRunner :=

// Android platform target (Same as targetSdkVersion on Gradle projects)
platformTarget := "android-24"

// Java compile options
javacOptions in Compile ++= "-source" :: "1.7" :: "-target" :: "1.7" :: Nil

// Libraries
libraryDependencies ++=
"" % "appcompat-v7" % "24.0.0" ::
"" % "runner" % "0.5" % "androidTest" ::
"" % "espresso-core" % "2.2.2" % "androidTest" ::
We don’t even need to use the SBT plugin to generate this template. If we prefer to craft our own minimum project, we could just create the project structure for SBT and Android manually, and add only the setup that we need.
Run the project
You will need to have an connected Android device or an running emulator.
Once this is done, the final step is to run the application from sbt:
> android:run
You can also run it from the terminal, instead of from SBT:
$ sbt android:run
More options on build.sbt
There are other interesting options that can be included in the build.sbt file. A few of them are:
// Application name
name := "scala_on_android"
// Min Android SDK version supported
minSdkVersion := "15"
// Override 'android:run', to use just 'run' instead
Add a comment...
Wait while more posts are being loaded