小编And*_*s K的帖子

Java 8构造函数引用的可怕性能和大堆占用空间?

我刚刚在我们的生产环境中遇到了相当不愉快的经历 OutOfMemoryErrors: heapspace..

我将这个问题追溯到我ArrayList::new在函数中的使用.

要通过声明的构造函数(t -> new ArrayList<>())验证这实际上比正常创建更糟糕,我编写了以下小方法:

public class TestMain {
  public static void main(String[] args) {
    boolean newMethod = false;
    Map<Integer,List<Integer>> map = new HashMap<>();
    int index = 0;

    while(true){
      if (newMethod) {
        map.computeIfAbsent(index, ArrayList::new).add(index);
     } else {
        map.computeIfAbsent(index, i->new ArrayList<>()).add(index);
      }
      if (index++ % 100 == 0) {
        System.out.println("Reached index "+index);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

运行方法newMethod=true;将导致方法OutOfMemoryError在索引达到30k后失败.随着newMethod=false;程序不会失败,但一直冲击直到被杀(索引容易达到150万).

为什么在堆上ArrayList::new创建如此多的Object[]元素会导致OutOfMemoryError如此之快?

(顺便说一下 - 当集合类型出现时也会发生HashSet …

java constructor out-of-memory java-8 method-reference

107
推荐指数
2
解决办法
3570
查看次数

较旧的日期被解析为夏令时,即使在Java中不是这样

我有一个问题,显示存储为多个日期的多个日期.我使用带有long参数的构造函数创建日期对象,然后将日期打印到PDF文件.

但是,与Windows相比,在Linux上运行程序时,我遇到了较旧的日期问题.

以此日期为例:1976年4月25日00:00:00(长值:199231200000L).如果我使用dateformater来显示日期,它将在Linux和Windows上以不同方式显示:

在Windows上:1976年4月25日00:00:00 CEST

在Linux上:1976年4月24日23:00:00 CET

文字代表.只需运行以下行即可显示:

DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL ).format( new Date( 199231200000L) )
Run Code Online (Sandbox Code Playgroud)

我使用Joda Time来获取此测试的日期值:

new org.joda.time.DateTime().withDate( 1976, 4, 25 ).withTime( 0, 0, 0, 0 ).toDate().getTime()
Run Code Online (Sandbox Code Playgroud)

为什么Windows将输出显示为CEST,Linux显示为CET?

java timezone date

5
推荐指数
1
解决办法
67
查看次数

自动完成功能在 Chrome 上不起作用

我在我们的网站上遇到了 Chromeautocomplete似乎无法正常工作的问题。它过去一直有效,但并非始终如一。

例如,我有一个input这样的字段:

<input type="text" class="gwt-TextBox Inputfield TextInputfield form-control" maxlength="40" name="name" autocomplete="name" width="0" style="max-width: 26em; width: 100%;">
Run Code Online (Sandbox Code Playgroud)

当我开始输入我的名字时,autocomplete下拉菜单会按预期打开,但是当我在autocomplete下拉菜单中选择我的名字时,除了下拉菜单关闭之外什么也没有发生。

问题截图

控制台中没有记录异常。

带有字段的页面在这里

html google-chrome autocomplete

5
推荐指数
2
解决办法
2万
查看次数