一个具有100个属性的对象占用的内存空间是否与100个对象相同,每个属性有一个属性?
为对象分配了多少内存?
添加属性时会使用多少额外空间?
在Java 8中if/throw else/return是否有更短的语法?java.util.Optional提供了在一个语句中完成此操作的方法,但它需要Optional为每个具有非空引用的调用创建一个实例.
public static MyEnum fromString(String value) {
MyEnum result = enumMap.get(value);
if (result == null)
throw new IllegalArgumentException("Unsupported value: " + value);
return result;
}
Run Code Online (Sandbox Code Playgroud)
public static MyEnum fromString(String value) {
return Optional.ofNullable(enumMap.get(value)).orElseThrow(
() -> new IllegalArgumentException("Unsupported value: " + value));
}
Run Code Online (Sandbox Code Playgroud) 我只是偶然发现了Java 8中的Optional类 - 我真的喜欢在我的代码中使用isPresent()方法调用替换一些空检查(字面意思是"值是否存在?")的方法.
我的问题是:这不会导致我的代码性能下降吗?我只是猜测简单的空检查可能会有点便宜而且我在字节码读取/解释方面还不是很好,所以我真的对你对这个主题的想法很感兴趣.
似乎这个问题应该已经有了答案,但我找不到重复的答案。
无论如何,我想知道社区对这样的Stream.map用例有何看法?
Wrapper wrapper = new Wrapper();
list.stream()
.map( s -> {
wrapper.setSource(s);
return wrapper;
} )
.forEach( w -> processWrapper(w) );
public static class Source {
private final String name;
public Source(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static class Wrapper {
private Source source = null;
public void setSource(Source source) {
this.source = source;
}
public String getName() {
return source.getName();
}
}
public void processWrapper(Wrapper wrapper) …Run Code Online (Sandbox Code Playgroud) 网上有一些文章提到了使用Stream-s 而不是旧的loop-s 的一些缺点:
But is there any impact from the GC perspective? As I assume (is it correct?) that every stream call creates some short-lived objects underneath. If the particular code fragment which uses streams is called frequently by the underlying system could it cause eventually some performance issue from the GC perspective or put extra pressure on GC? Or the impact is minimal and could be ignored most of the time?
Are there …
java performance garbage-collection premature-optimization java-stream
假设我有一个 java.util.Collection 想要循环。通常我会这样做:
for(Thing thing : things) do_something_with(thing);
Run Code Online (Sandbox Code Playgroud)
但是假设这是在一些到处使用的核心实用方法中,并且在大多数地方,集合是空的。那么理想情况下,我们不希望仅仅为了执行无操作循环而对每个调用者强加迭代器分配,并且我们可以重写如下内容:
if(things.isEmpty()) return;
for(Thing thing : things) do_something_with(thing);
Run Code Online (Sandbox Code Playgroud)
如果是列表,则更极端的选择things是使用 C 样式for循环。
但是等等,Java 转义分析应该消除这种分配,至少在 C2 编译器处理此方法之后。所以应该不需要这种“纳米优化”。(我什至不会用微优化这个词来形容它;它对于这个来说有点太小了。)除了......
我一直听说逃逸分析是“脆弱的”,但似乎没有人谈论过什么会导致它变得混乱。直觉上,我认为更复杂的控制流将是最值得担心的,这意味着 for-each 循环中的迭代器应该被可靠地消除,因为那里的控制流很简单。
这里的标准响应是尝试进行实验,但除非我知道其中的变量,否则很难相信我可能想从这样的实验中得出的任何结论。
事实上,在这篇博客文章中,有人尝试了这样的实验,三分之二的分析器给出了错误的结果:
http://psy-lob-saw.blogspot.com/2014/12/the-escape-of-arraylistiterator.html
我对晦涩难懂的 JVM 魔法的了解比该博客文章的作者要少得多,而且很可能更容易被误导。
使用不可变对象编写程序会导致性能问题吗?如果给定的对象是不可变的,并且我们需要以某种方式更改其状态,则必须将其映射到状态稍有更改的新对象。因此,我们会发现自己处于这样一种情况:创建了很多对象,这些对象堵塞了内存,并且据我了解,这可能会给垃圾收集器带来问题。我所描述的情况是否正在发生,或者是否存在我不知道的关于该主题的某些方面?
根据这个问题,find和之间有很大的区别matches(),仍然以某种形式提供结果。
作为一种实用程序,该toMatchResult函数返回matches()操作的当前结果。我希望我的假设(1)是有效的。(正则表达式在这里)
String line = "aabaaabaaabaaaaaab";
String regex = "(a*b)a{3}";
Matcher matcher = Pattern.compile(regex).matcher(line);
matcher.find();
// matcher.matches();(1) --> returns false because the regex doesn't match the whole string
String expectingAab = matcher.group(1);
System.out.println("actually: " + expectingAab);
Run Code Online (Sandbox Code Playgroud)
不幸的是,以下方法无效(例外:未找到匹配项):
String line = "aabaaabaaabaaaaaab";
String regex = "(a*b)a{3}";
String expectingAab = Pattern.compile(regex).matcher(line).toMatchResult().group(1);
System.out.println("actually: " + expectingAab);
Run Code Online (Sandbox Code Playgroud)
这是为什么?我的第一个假设是它不起作用,因为正则表达式应该匹配整个字符串;但同样的异常也与字符串值一起抛出aabaaa......
当然,匹配器需要使用 设置为正确的状态find(),但是如果我想使用 oneliner 呢?我实际上为此实现了一个实用程序类:
protected static class FindResult{ …Run Code Online (Sandbox Code Playgroud) java ×8
java-8 ×3
java-stream ×2
performance ×2
footprint ×1
immutability ×1
jit ×1
jvm ×1
jvm-hotspot ×1
memory ×1
one-liner ×1
regex ×1