Serializable, cloneable and memory use in Java?

This may not address all of your questions, but is a way in which serialization can significantly increase memory usage: java.sun.com/javase/technologies/core/ba....

This may not address all of your questions, but is a way in which serialization can significantly increase memory usage: java.sun.com/javase/technologies/core/ba.... In short, if you keep an ObjectOutputStream open then none of the objects that have been written to it can be garbage-collected unless you explicitly call its reset() method.

1 This is a good lead, provided that the objects in question are being actually serialized, because the original post only suggests memory increase by making the classes serializable and the developer performed tests on non-serializable dummy classes and determined memory footprint was smaller (we yet don't know how this evaluation was carried out though), but if this is case, then the root case should be something else. Honestly, I am too inclined to believe that your explanation is the most logical one so far. – edalorzo Apr 19 at 18:00.

So, I found the answer. It is a memory leak in my code. Had nothing to do with Serializable or Cloneable.

This code is trying to parse a file. Each line contains a set of values which I am trying to extract. Then, I keep some of those values and store them in a HashMapX or some other structure.

The core of the problem is here: String fields = s.nextLine(). Split("\\s+"); String docName = fields1; and I propagate it here: documents. Put(docName,thetas); What happens is that docName is a reference to an element in an array (fields) and I am keeping that reference for the life of the program (by storing it in the global HashMap documents).

As long as I keep that reference alive, the whole String fields cannot be garbage collected. The solution: String docName = new String(fields1); // A copy, not a reference. Thus copying the object and releasing the reference to the array element.In this way, the garbage collector can free the memory used by the array once I process every field.

I hope this will be useful to all of those who parse large text files using split and store some of the fields in global variables. Thanks everybody for their comments. They guided me in the right direction.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions