小编sun*_*sun的帖子

怎么可能'originalValue.length> size'在String构造函数中发生?

以下是String类 的构造函数

public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
    // The array representing the String is bigger than the new
    // String itself.  Perhaps this constructor is being called
    // in order to trim the baggage, so make a copy of the array.
        int off = original.offset;
        v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
    // The array representing the String is the same
    // size …
Run Code Online (Sandbox Code Playgroud)

java string

6
推荐指数
1
解决办法
165
查看次数

调用Hashmap.remove()后立即发生垃圾收集吗?

Java代码如下:

Random r = new Random(1234697890);
HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
List<Integer> list = new ArrayList<Integer>();

for(int i=0;i<100000;i++){
    for(int j=0;j<1000;j++){
        list.add(r.nextInt(100000));
    }
    map.put(i, list);
    map.remove(i);
}
Run Code Online (Sandbox Code Playgroud)

i达到37553时,java.lang.OutOfMemoryError: Java heap space发生了.
似乎垃圾收集不会在循环中发生.
现在我想知道如何解决这个问题.

java garbage-collection out-of-memory

6
推荐指数
2
解决办法
1933
查看次数

标签 统计

java ×2

garbage-collection ×1

out-of-memory ×1

string ×1