简体   繁体   中英

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.

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. 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. If the JIT decides: not important enough to inline, then we are talking about nanosecods anyway.

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. 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.

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. 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.

In both cases, you create 3 instances of Value that are stored in a 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 :

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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