2

I'm coming from C/C++ background, I wanted to know if "new" worked in a similar fashion as in those languages. For example, for performance gains in C++ one would allocate a large amount of memory up front and use this memory.

Phaino
  • 501
  • 1
  • 5
  • 10
  • 6
    In Java, `new` is the only way of creating objects, so frankly, it doesn't matter. You have no choice but to use it. – Ben Steffan May 09 '17 at 15:58
  • 1
    in java, you do not allocate memory up front. So it seems an obsolete question – IAmGroot May 09 '17 at 15:58
  • 3
    This is a legit question. There are alternatives to `new` which is to pool objects. – Steve Kuo May 09 '17 at 16:01
  • 1
    And with respect to object pooling, this related question may be of interest: http://stackoverflow.com/questions/680514/object-pooling. In particular, take note of this assertion from the second answer: "memory allocation in java is free (the cost was close to 10 cpu instructions, which is nothing). So reducing the creation of objects only saves you the time spent in the constructor." – John Bollinger May 09 '17 at 16:05
  • No, it's not. C++ doesn't have a GC. I would say it's different animal. The memory model in C++ didn't have the notion of generations when I last looked at it. I would call new and sleep at night. Let the GC do its job. – duffymo May 09 '17 at 16:07
  • @BenSteffan you have a choice between one `new byte[1024 * 1024]` or `new byte[1024]` 1024 times (for example) – slim May 09 '17 at 16:14
  • 1
    The duplicate answer is about the same topic but doesn't actually answer the question. – Persixty May 09 '17 at 16:28
  • well then, let's re-open it. – Mike Nakis May 09 '17 at 17:56
  • Then the question should explain specifically why the dup is wrong. Or maybe a corresponding answer could be added to the other question.. – GhostCat May 10 '17 at 03:54

1 Answers1

6

Memory allocation is vastly different between these languages; this a big subject and it cannot be reduced to a simplistic question like whether new in java works "in a similar fashion" as in C++.

To give you a simplistic answer, it certainly does not work in a similar fashion because in Java you never need to delete.

To make you happier, let me also add that new in Java is purported to be a lot faster than in C++, because the runtime does not need to maintain linked lists of allocated and free blocks, and it does not have to search for a gap that is large enough to contain the block you need. Also, it does not suffer from the memory fragmentation problems that you may encounter with C++.

Most of the time, (if you are running under plentiful memory conditions, and in modern days we usually are,) the java runtime simply has a pointer pointing at the boundary between the allocated memory and the free memory, it takes a copy of that pointer, it adds the number of bytes that you want to the pointer, and it returns the copy to you. The overhead comes later, during garbage collection.

So, overall, java tends to give you memory quicker than C++ does, but it adds a certain overhead dispersed over your entire runtime due to frequent and complicated garbage collection. This overhead is unavoidable, and somewhat unpredictable, but on modern machines it is mostly (though not always) imperceptible.

The bottom line is that from the start, Java aimed to free programmers from having to worry about memory allocation, and to a very large extent it has been very successful in doing so. It is only under extremely rare, highly exceptional circumstances, that java programmers need to worry about pre-allocating objects, implementing their own object pools, etc. All these things are mostly non-issues in java.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Well you don't need to `null` out values unless your code contains a [Retain Cycle](https://en.wikipedia.org/wiki/Reference_counting#Dealing_with_reference_cycles) – Alex S May 09 '17 at 16:15
  • Right you are, I've been doing too much swift lately. – Alex S May 09 '17 at 16:21