我是一名c ++开发人员,但我最近做了一点Java.我正在工作的这个项目是由开发人员完成的,很久以后我就一直在寻找他正在通过做奇怪事情来解决垃圾收集问题的事情.
Case和point他实现了自己的字符串类,以避免GC减速
应用程序的这一部分采用大型二进制文件格式并将其导出到csv.这意味着为文件中的每一行构建一个字符串(数百万).为了避免这些临时字符串对象,他创建了一个字符串类,它只有一个重复使用的大字节数组.
/**
HACK
A Quick and Dirty string builder implementation optimized for GC.
Using String.format causes the application grind to a halt when
more than a couple of string operations are performed due to the number of
temporary objects allocated while formatting strings for drawing or logging.
*/
Run Code Online (Sandbox Code Playgroud)
这实际上有帮助吗?这真的需要吗?这比仅仅在循环外声明一个String对象并将其设置在循环中更好吗?
该应用程序还有一个哈希映射,包含值的双精度数.地图中的键是相当静态的,但值经常变化.由于害怕GC上双打,他将myDouble类用作hashmap的值
/**
* This is a Mutable Double Wrapper class created to avoid GC issues
*
*/
public class MyDouble implements Serializable {
/**
*
*/
private static …Run Code Online (Sandbox Code Playgroud)