Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

garbage collect objects after lazy values have been calculated

in my current project I am processing a quite big amount of data and the processing of the data should be both memory efficient and computationally performant. Every item has some meta-data that can be read very fast and is almost always interesting. Additionally to that every item has the actual data that is comparatively rarely read but the reading and especially the parsing is very time consuming. Therefore it seams natural that the parsing of the data should only be done if it is actually requested.

For that purpose I was thinking of lazy values:

class Item(metaData: MetaData, dataString: String) {
    lazy val data = parse(dataString)
}

Now the data is only parsed if it is actually requested. The problem is now, that the dataString and the parsed data is kept in memory. As far as I can see, "dataString" cannot be accessed anymore as soon as "data" has been called (or is there?) and it can therefore be garbage collected. Unfortunately this seams not to happend.

Is there a way to solve the problem in a different way or to give the garbage collector a hint to garbage collect the dataString here?

like image 601
Zwackelmann Avatar asked Apr 27 '13 15:04

Zwackelmann


People also ask

What are the limitations of garbage collection?

Drawbacks of garbage collection in Java Garbage collectors bring some runtime overhead that is out of the programmer's control. This could lead to performance problems for large applications that scale large numbers of threads or processors, or sockets that consume a large amount of memory.

When an object is eligible for garbage collection?

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null.

What is lazy garbage collection?

One such improvement suggested by the Garbage Collection Handbook is lazy sweeping. The basic idea is that instead of having the collector thread sweep the entire heap at once when tracing is finished, each mutator thread will sweep its own heap incrementally as part of allocation.

How does the garbage collector determine when an object is no longer needed?

When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.


1 Answers

You just need a little bit more tooling:

class Item(dataString: String) {
  private var storedData = dataString
  lazy val data = {
    val temp = parse(storedData)
    storedData = null
    temp
  }
}

An extra reference to dataString is not kept because you never refer to it outside of the constructor (which sets storedData), and the reference you store in storedData is nulled out once you use it, so the string is then free to be GCed.

like image 135
Rex Kerr Avatar answered Sep 29 '22 01:09

Rex Kerr