简体   繁体   English

Java使用new,性能和内存重新初始化java对象

[英]Java re-initializing java object with new , performance and memory

I want to know the drawback of writing below code using new for reinitializing object every time to create different object value. 我想知道每次使用new编写下面的代码来重新初始化对象以创建不同的对象值的缺点。

List <Value> valueList = new ArrayList<>;

Value value = new Value();    
value.setData("1");
valueList.add(value);

value = new value();
value.setData("2");
valueList.add(value);

value = new value();
value.setData("3");
valueList.add(value);

or a method could be added to return a value object similar to: 或者可以添加一个方法来返回类似于以下内容的值对象:

private Value getData(String input){
  Value value = new Value();
  value.setData(input);
  return value;
}

List <Value> valueList = new ArrayList<>;

valueList.add(getData("1"));
valueList.add(getData("2"));
valueList.add(getData("3"));

Code wise the second approach looks better for me. 在代码方面,第二种方法对我来说看起来更好。

Please suggest the best approaches based on memory and performance. 请根据内存和性能提出最佳方法。

Both options are creating 3 objects and adding them to a list. 这两个选项都将创建3个对象并将它们添加到列表中。 There is no difference for memory. 内存没有区别。 Performance doesn't matter. 性能无所谓。 If this code is executed often enough to "matter", the JIT will inline those method calls anyway. 如果执行此代码的频率足以“重要”,JIT仍将内联那些方法调用。 If the JIT decides: not important enough to inline, then we are talking about nanosecods anyway. 如果JIT做出决定:不够内联,那么我们无论如何都在谈论纳秒级

Thus: focus on writing clean code that gets the job done in a straight forward way. 因此:专注于编写简洁明了的代码,从而以一种直接的方式完成工作。

From that perspective, I would suggest that you rather have a constructor that takes that data; 从这个角度来看,我建议您宁愿有一个使用该数据的构造函数。 and then you can write: 然后您可以编写:

 ValueList<Value> values = Arrays.asList(new Value("1"), new Value("2"), new Value("3"));

Long story short: performance is a luxury problem. 长话短说: 性能是一个奢侈品问题。 Meaning: you only worry about performance when your tests/customers complain about "things taking too long". 含义:仅当测试/客户抱怨“事情花太长时间”时,您才担心性能。

Before that, you worry about creating a good, sound OO design and writing down a correct implementation. 在此之前,您担心要创建良好的,合理的OO设计并写下正确的实现。 It is much easier to fix a certain performance problem within well built application - compared to getting "quality" into a code base that was driven by thoughts like those that we find in your questions. 与在像我们在您的问题中发现的想法所驱动的代码库中添加“质量”相比,在构建良好的应用程序中解决某个性能问题要容易得多。

Please note: that of course implies that you are aware of typical "performance pitfalls" which should be avoided. 请注意:当然,这意味着您已经意识到应避免的典型“性能陷阱”。 So: an experienced Java programmer knows how to implement things in an efficient way. 因此:经验丰富的Java程序员知道如何高效地实现事物。

But you as a newbie: you only focus on writing correct, human readable programs. 但是您是新手:您专注于编写正确的,人类可读的程序。 Keep in mind that your CPU does billions of cycles per second - thus performance is simply OK by default. 请记住,您的CPU每秒执行数十亿个周期-因此默认情况下,性能就可以了。 You only have to worry when you are doing things on very large scale . 当您大规模地做事时,您只需要担心。

Finally: option 2 is in fact "better" - because it reduces the amount of code duplication. 最后:选项2实际上“更好”-因为它减少了代码重复的数量。

In both cases, you create 3 instances of Value that are stored in a List. 在这两种情况下,您都将创建3个Value实例,这些实例存储在一个List中。
It doesn't have sensitive differences in terms of consumed memory. 就内存消耗而言,它没有敏感的差异。

The last one produces nevertheless a cleaner code : you don't reuse a same variable and the variable has a limited scope. 然而,最后一个产生的代码更简洁 :您不会重复使用相同的变量,并且该变量的作用域有限。
You have indeed a factory method that does the job and returns the object for you. 实际上,您确实有一个工厂方法来执行该工作并为您返回对象。
So client code has just to "consume" it. 因此,客户端代码只需“消费”它即可。

An alternative is a method with a varargs parameter : 另一种方法是使用varargs参数的方法:

private List<Value> getData(String... input){
    // check not null before
    List<Value> values = new ArrayList<>();
    for (String s : input){
       Value value = new Value();
       value.setData(input);   
    }
    return values;   
}

List<Value> values = getData("1","2","3");

There is no difference in memory footprint, and there's little difference in performance, because method invocations are very inexpensive. 因为方法调用非常便宜,所以内存占用量没有差异,性能也几乎没有差异。

The second form of your code is a better-looking version of the first form of your code, with less code repetition. 代码的第二种形式是第一种形式的外观更好的版本,减少了代码重复。 Other than that, the two are equivalent. 除此之外,两者是等效的。

You can shorten your code by using streams: 您可以使用流来缩短代码:

List<Value> = Arrays.asList("1", "2", "3").stream()
    .map(Value::new)
    .collect(Collectors.toList());

Every time you calling new operator to create an object, it allocates spaces for this object on heap. 每次调用new运算符创建对象时,它都会在堆上为此对象分配空间。 It doesn't matter if you do it with 1st approach or 2nd approach, this objects are allocated to heap space the same way. 使用第一种方法还是第二种方法都没关系,该对象以相同的方式分配给堆空间。

What you need to understand thou is a life-cycle of each object you creating and terms like Dependency, Aggregation, Association and Full Composition. 您需要了解的是您创建的每个对象以及诸如依赖性,聚合,关联和完全组合之类的术语的生命周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM