The musings and ramblings of a software craftsman

Let’s talk about memory!

As mentioned in the “About” section of the blog, I’m a developer and you could say I’m mainly focused on Java (though I’m interested in other languages such as Python, C++, Haskell and Scala) . One of the main reasons a lot of people choose Java is because it’s garbage collector, which, broadly speaking, is in charge of counting references to the variables in your program and, makes sure the memory they use is freed once the counter reaches 0 – the algorithms that decide when an object is “collected” are much more complex than what I’ve described but, for simplicity’s sake we’ll leave it like that – something that in other languages, like C++, is in the programmers hands.

So, can I forget about memory management ?! , I hear you ask in jubilation. The answer is yes and no; you see, for most tasks you won’t need to care about memory allocation and actually, I know programmers that have never had the need to know what volatile is. Now, some of you might remember an article published nearly 8 years ago, titled “The Free Lunch is Over” . In said article, the author commented on the fact that processor’s clocks weren’t getting any faster, whereas the number of cores they packed was increasing, therefore, any performance gains would not come simply as a result of a hardware upgrade any more, unless your software was designed to work with multiple cores. However, concurrent programing is not easy and it requires knowing very well what your memory is doing, as once you start sharing things among threads, things become very complicated very quickly,  this is, of course, if you don’t know what you’re doing. Sadly, I can’t claim – yet – that I know what I’m doing but, I intend to explore this concept further, in the context of the Java programming language and, publish my experiments here.

So I’m going to start now, with a simple question, what is the heap and what is the stack? . Surely, most programmers have heard of those two concepts but, let’s recap for a moment. All java programs run inside the JVM, and when the virtual machine is invoked to run your program, it will ask the hosting operating system some memory for running both itself and your program, that lump of memory is called The Heap*. So, when in your code you use the new  operator , to create an instance of one of your classes or an array or you declare a static variable, the JVM will allocate the memory needed for the object/array in the heap (note that the reference pointer to the object will be allocated in the stack); the heap itself is a bit more complicated than “a lump”, it’s actually subdivided further into 3 sections (‘Young Generation’, ‘Tenured Generation’ and ‘Permanent Generation’), this subdivision has to do with the way the garbage collector works and therefore, I will not talk about it now.

An OutOfMemoryError occurs when your program exhausts the maximum amount of memory allocatable by the JVM for the heap, see bellow for an in code example:

/**
 * Run this code with the following JVM options: -Xmx2m -Xms2m
 * This tells the JVM to allocate only 2Mb of heap space, for a maximum of 8Mb.
 * (that's in my 64bit linux desktop, it might vary in your system).
 */
public class Main {

    public static void main(String[] args) { 
            Set<String> collection = new HashSet<String>();
            while(true) {
                 collection.add(getReallyLongString(500));
            }
    }

    private static String getReallyLongString(int numberOfCharacters) {
        String longstring = new String();
        for(int i = 0; i < numberOfCharacters; i++) {
            longstring = longstring.concat("a");
        }
        return longstring + System.currentTimeMillis();
    }

}

If the heap is where your objects get allocated, what is The Stack then?. Well, not everything in your java application are objects, right?, what about primitive variables?, int, long, double, char, byte, boolean, they too need allocation. The primitive types don't get allocated in the heap (unless they're attributes of an object), they get allocated in the stack, however,  that's not the only thing the stack is used for,  it also keeps track of method calls, stores method arguments, local variables and return values. So let's say that you have 2 methods #myFirstMethod() and #mySecondMethod(), myFirstMethod calls mySecondMethod at some point during it's execution. In terms of the JVM this means that when myFirstMethod is called, all it's local primitive - non static - variables (this includes object references) will be neatly allocated and pushed into the stack, until the call to mySecondMethod is found, at that point, the local variables of mySecondMethod will be allocated and pushed into the stack, on top of the entry for myFirstMethod (it's called stack for a reason), so when mySecondMethod finishes, it will be popped out of the stack then myFristMethod will be popped and so on and so forth until the call by the very first method is unrolled (this is always Main()  in case you hadn't guessed it yet), and your program finishes.

A StackOverflowError happens when the stack's space is exhausted. When the memory address for the stack is reserved, it's done downwards, this means the address pool will go from say 0xFFFFFF to 0x000001, since every time you call a function, it's address, parameters and local variables are stored in a new frame in the stack, recursive functions with the wrong termination condition,  are natural targets for this kind of error. If you run the code snippet below, you'll get a StackOverflowError almost instantly.

public class Main {

    public static void main(String[] args) {        
            add(1);        
    }

    private static int add(int a) {
        return add(a + a);
    }
}

Now, here is an interesting question. When your program starts, the JVM creates a thread in which to run your Main() function, and so it creates a stack for that thread but, what if you started another thread within your Main() function?, well, as expected, the JVM would create another stack for that thread but, what if you wanted to use an object allocated in the first thread in this new thread?  and if the first thread, changed the object's state while the second thread is running? what happens then?, that's when things start to get tricky and, it will be the topic of posts to follow.

*Note: I've  also seen the heap being referred to as "main memory" in other articles.

Socializing healthcare, in essence, means making bad health everyone’s problem instead of a few’s profit. That is the reason why the American healthcare industry is so adamant in remaining largely private. In a society based on profit, the only problems that are solved are those turning a gain for whoever finds an answer, hence, solving obesity is not a priority, because, widening waistlines are much more profitable than narrowing ones.

It all makes sense when you come to think about who benefits from fatter people rather than thinner people. The textile industry, as bigger sizes require more fabric, the healthcare industry, because more obese people translates into more people with health issues, derived from being overweight, the flying industry, as fat people have to pay more for their seats and obviously, the food industry, as ever growing bellies mean more sales.

It has actually come to a point where the lobbying power of any of those industries is able to cross frontiers, making our elected governments virtually prisoners, servants of those who, with the help of everyone’s got to where they are today.

But these people are victims too, victims of a society created by ourselves in such a way that the only viable life-path stems from robbing others. If I, say own a shoe shop, the only way I’ve got to prosper is to sell more and more shoes but, If I make shoes that last a long time, people won’t come back often enough for me to be profitable, therefore, I need to make lower quality shoes (or find a way of making them cheaper, e.g. by getting them produced with cheap labour) so that they break more more often hence forcing people to come back.

Our actual economic/banking system derives from that very same principle, as all debt produced has an interest attached, now, here’s a story. Imagine you are the first bank in the world and you perform the first 2 loans in history, you lend 100 coins to John and 100 coins to Elisabeth, you lend for a profit so each of those loans has, say, a 5% interest attached, which means that both John and Elisabeth will have to pay you 105 coins at the end of the term of the loan, but, here’s the trick, you lent (i.e. put in circulation) 200 coins and expect 210 in return, so the only way for either of the borrowers to repay the loan, is to get the extra 5 coins from the other, who then won’t be able to repay the loan except by asking another loan. You’ve created a deficit that can never be repaid unless by taking it from someone else.

 Sadly enough, the society I’ve described in the paragraphs above is more akin to the animal kingdom than to a civilized one. I sincerely hope that the coming generations will find out how to build something better because, we are so (quoting The Shawshank Redemption) institutionalized that is very unlikely that we’ll do anything other than complain.

Nunca he sido creyente de todo el asunto de los astros, signos del zodíaco y demás temas esotéricos, en realidad nunca he sido creyente, punto;  sin embargo no se me ocurre otro nombre para lo que me ha ocurrido a lo largo de toda la semana. A pesar de haber planeado con antelación mi mudanza a Londres, haber hablado con la empresa etc… todo se ha ido al garete en cosa de 2 días.

El Viernes a la mañana me llaman desde la academia de conducir para decirme que no voy a poder presentarme al examen práctico que tenía previsto para el Lunes (justo antes de coger el vuelo para Londres), porque la secretaria se equivocó al enviar la documentación a la DGT y puso que me quería examinar el carné A en lugar del B (y eso que ya sería mi tercer intento).

El mismo día, pero unas cuantas horas después, a eso de las 7 de la tarde, empiezo a notar molestias en la garganta, no le doy mayor importancia y me tomo un par de Strepsils con Lidocaína. A las 2 de la mañana ya tenía fiebre; fiebre que continuaría todo el Sábado y el Domingo, hasta que a mediodía, decido ir a urgencias; me diagnostican Bronquitis y el médico no se olvida de comentarme amablemente, que ni de coña paso por la aduana de un aeropuerto internacional (como los de Londres) con fiebre (y menos ahora con toda la histeria colectiva sobre la Gripe A). Así que no me queda mas remedio que cambiar la fecha de salida de mi vuelo hasta el Miércoles como poco, pero oh!, mi billete de avión es de una “low cost” (notense las comillas) para el que no hay reembolso y un cambio de fecha significa a efectos, comprar otro billete.

La nota de buen humor y cachondeo la pusieron todos mis amig@s con el fiestón de despedida que montaron el Viernes, no creo que eche muchas cosas de menos, pero a ell@s seguro que sí.

En fin, una semana para olvidar :(

——————————————————-

I’ve never been a believer of all that stars, zodiac signs and such esoteric themes, well, I’ve never been a believer, period. However, I can’t think of any other name for what has happened to me this week. Even though I had planned my moving to London in advance, having talked with the recruiter, the company and so… everything has gone bust during the last 2 days.

Friday in the morning I recieve a call from the driving accademy only to tell me that due to an administrative error (of theirs) It won’t be possilbe for me to take the practical exam on Monday (just a few hours before taking the flight to London).

The very same day but a few hours later, like at 7 pm or so, I start noticing a slight pain in my throat, I take a pair of Strepsils and don’t pay it much more attention; At 2 am I was on fever; fever that would go on during Saturday and Sunday until evening that I decide to go to the hospital where I get diagnosed bronchitis; the doctor kindly reminds me that under no circumstances will I be allowed through an international airport customs (like London’s) with fever (god bless thermal cameras), and since the Swine Flu histeria even less. So, I don’t have any other option but to change the day of my flight, but oh!, mine is a “Low Cost” (notice quotation marks) ticket, for which there is no refund and  a date change means effectively buying a new one.

The funny note came from all my friends who prepared an incredible farewell party; I don’t think I’ll be missing a lot of things from Spain but they will be one of them without doubt.

Anyway, a week to be forgotten :(

Lo básico de Android

Lo básico de Android

Resulta que me he puesto a trastear recientemente con el nuevo SO de Goolge para dispositivos móviles, conocido como Android, y me ha dado por hacerme este esquemilla a modo de recordatorio de los aspectos fundamentales del desarrollo de aplicaciones para esta plataforma, si alguien lo encuentra util puede utililzarlo tranquilamente bajo licencia Creative Commons by-nc-sa.

Haced click en la imagen para verla a tamaño completo y poder descargarla.

It happens that I’ve recently begun to play with Google’s new OS for mobile devices, known as Android and I did this little scheme covering the plattform’s development basics, If anyone finds it usefull they can carelessly use it under Creative Commons by-nc-sa license.

Do click onto the image to see it in full size and to download it.

No llevo mucho tiempo trabajando en proyectos de software de forma profesional, así que es probable que me equivoque o que mi experiencia no sea lo suficientemente amplia como para dar una opinión sólida, pero ahí va.

Lo cierto, es que hasta ahora he visto una serie de fallos fundamentales en casi todos los proyectos en los que he participado, y creo que en parte, se debe a la opinión generalizada por parte del empresariado Español, de que (simplificando) “Eso de la Informática lo hace cualquiera”, cuando en realidad se trata de un proceso de ingeniería, tal como construir un camino, o un edificio. A saber, los fallos son:

  1. Tomas de requisitos escasas o cuando menos, poco detalladas. No, 4 párrafos en un folio no son suficientes para que el cliente te diga lo que necesita que haga la aplicación. Eso da pié a que tengas un equipo de desarrolladores parado por falta de información, y a que el cliente incluya funcionalidades nuevas y cambios allá donde le de la gana y cuándo le de la gana. Si se queda en hacer X, se hace X, no X y aprovechando que el Duero pasa por Valladolid, me haces X + Y + Z y te pago lo mismo que solo por X.
  2. Inexistencia total de un sistema de pruebas estandarizado. Todavía no he visto usar en ningún proyecto frameworks de pruebas unitarias (p.ej: jUnit); la mayoría de las veces, las pruebas (y por ende la comprobación de la calidad del producto) se hacen al final del desarrollo y corriendo, lo que no suele dejar al cliente final muy contento, ya que le da la sensación de que consideras que su proyecto es algo de segunda categoría que no merece la misma atención al detalle que el resto.
  3. Escasa flexibilidad frente a cambios. Cada vez que el cliente solicita un cambio, este, tarda demasiado en aplicarse, provoca nuevos fallos en código que ya funcionaba y por norma general tiene demasiadas ramificaciones. No puede ser que cambiar un pequeño proceso de tu aplicación, requiera cambiar media docena de clases, 2 archivos de configuración y probar toda la aplicación de nuevo; eso no es eficiente.
  4. Fechas de entrega poco realistas. Aún estoy por presenciar, un proyecto entregado a tiempo. No se trata de vender como sea y en el tiempo que sea, España no puede competir en tiempo y costo, eso pueden hacerlo China o India, que tienen mano de obra cuasi esclava. España ha de competir en funcionalidad y calidad. ¿Qué prefieres vender, scooters o mercedes?.

Ahora mismo no se me ocurren más, pero si alguien quiere aportar, que mande un e-mail o comente.

It has not passed many time since I began working in IT projects professionally, which means that I may be wrong or simply that my experience it’s just “not enough” to be able to make a solid opinion, but here it goes.

The truth is that , until now, I have witnessed a series of fundamental errors in almost every project in which I have been involved, and I think that at least in part, the generalized opinion of the majority of the spanish enterprises that (simplifying) “anyone can do that thing of the IT” is to be blamed; because in fact, it is a true engineering process, like building a road or a house. Now for the errors:

  1. Sparse requirements gathering, or at least not enough detailed. No, four handwritten paragraphs do not suffice for the client to tell us what he wants the application to do. That gives cause for the development team to sit idle while waiting for needed information and, for the customer to ask for new functionalities and changes wherever and whenever they see it. If you agree on developing X, you  do X and not X + Y + Z by arguing that requirements have not yet been met.
  2. Nonexistent standardized testing method. I have yet to see a unit testing framework (like jUnit) used in a project; most of the time, testing (and as such, projects quality checking) is done in the end and on the run, which does not leave the customer very satisfied, mainly because he is left with the feeling that you take their application as a second class one and thus you are not putting the same attention to detail as you do with the rest.
  3. Little flexibility towards changes. Each time the customer asks for a change, this , needs a lot of time to be implemented, introduces errors in already working code and usually has too many ramifications. Modifying a small business procedure, should not trigger changes in half a dozen classes, two configuration files and testing the whole program again, that is simply not efficient.
  4. Unrealistic deadlines. I have not yet seen a project handed on time. Seriously, it is not about selling at any cost; Spain can not compete in time or budget; China and India can because their workforce is near enslavery. Spain must compete in quality and functionality. What do you want to sell? scooters or Mercedes?.

I can not think of more errors right now,  but if anybody wants to add something, just e-mail me or post a comment.

Relaciones (Relationships)

Esta mañana, hablando con dos de mis compañeros de trabajo a la hora del café, salió el tema de las relaciones de pareja y cómo de diferentes son en el País Vasco (de donde soy y donde vivo actualmente). Llegué a la conclusión de que cuanto más hacia el sur se dirige uno, más abierta es la gente y permancer mucho tiempo sin pareja (estable o no) es algo extraño y poco habítual, calculo que más de un año sin pareja ya se considera una rareza. Por otro lado aquí en el norte, uno de mis compañeros lleva entre cuatro y cinco años sin salir con nadie (exceptuando sus amigos claro) y yo mismo llevo 9 años, y nadie lo ve inusual.

This morning, while talking with my coleagues at work in the cofee break time, relationships just sprung up and we began talking about how diferent are from the ones in Vasque Country (from where I am and where I currently live). I concluded that the more you head towards the south the more open the people are, and to linger much time without someone (long term or not) is rare and uncommon. I guess that being alone for more than one year it’s considered strange. On the other side, here in the north, one of my coleagues has already been four or five years alone (not taking his friends into account) and it’s been nine years for me. In spite of this, noone here thinks it’s unusual.

Antes que nada comentar que he decidido que a partir de ahora cuando escriba (que ya se que no es mucho), lo haré tanto en Castellano como en Inglés.

El asunto es que hay algo que me carcome por dentro, y es que me estoy dando cuenta de que a medida que pasa el tiempo y los años, me estoy haciendo más y más insensible, a todo. A veces veo cosas, noticias, fotografías, vídeos… que creo, deberían despertar algún tipo de sentimiento y sin embargo no provocan ni el más mínimo atisbo de emoción. ¿Me estaré haciendo de piedra?, ¿será la soledad?, la verdad es que no tengo nada claro porqué me está ocurriendo, pero el caso es que con sinceridad, me doy miedo, cada vez me importa todo menos (por no decir que nada). ¿Os pasa a algúno/a de vosotr@s?.

P.D: Si alguien encuentra fallos en mi traducción, estaría encantado de que me corrigiera.

Before anything, I want to say that I’ve decided that from now on, whenever I post (and I know I don’t do it often), I’ll do both in English and Spanish.

The issue is that there is something eating me in my inner, and it is that I’m noticing that as the time and the years go by, I’m becoming more and more insensible, to everything. Sometimes I see things, news, photographs, videos… witch I think, should awake some kind of feeling inside and however, don’t provoke even the smallest sparkle of emotion. Am I becoming stone? could it be loneliness?. The truth is that can’t see why is this happening but the case is that sincerely, I scare myself, each time I care less (not to say nothing). Does it happen to any of you?.

P.S: If someone sees a mistake in the translation, I would be glad to be told.

Follow

Get every new post delivered to your Inbox.