Hot answers tagged

25

The problem with stacks is that you can't "free" memory unless it is on top of the stack. For instance, say you allocated 3 things of varying sizes: a = allocate(2000000); // 2000000 bytes b = allocate(1); c = allocate(5000000); The stack would have a on the bottom, b in the middle, and c on top. This becomes problematic if we want to free b: free(b); // ...


23

There are two different memory limits. The virtual memory limit and the physical memory limit. Virtual Memory The virtual memory is limited by size and layout of address space available. Usually at the very beginning is the executable code and static data and past that grows the heap, while at the end is area reserved by kernel, before it the shared ...


23

This is an interesting question, and the answer is complex. Overall, I think it is fair to say that the JVM garbage collector is very well designed and extremely efficient. It's probably the best general purpose memory management system. C++ can beat the JVM GC with specialised memory allocators that are designed for specific purposes. Examples might be: ...


16

Allocating a variable on the stack and deallocating it is a simple addition and subtraction of the stack pointer. Given that it happens anyway when entering a function means that local variables are so cheap that trying to optimize them to anything else will generally incur more cost. Putting it in the data segment will incur a cache cost, the stack will ...


13

Fragmentation comes from memory that is unusable. Dynamic allocation is similar to playing Tetris - if you play fast you end up with holes (fragmentation), and you can't predict what type block is going to fall down next. With dynamic allocation, you can't predict when and what memory is going to be freed - imagine playing Tetris where blocks disappear ...


12

The CLR standard does not require a stack or a heap, so lets get that out of the way first. But C# implemented on paper isn't very useful. I describe here the implementations we can run code with "in practice", like the Microsoft C# or Mono C#. Regardless, the method and local variables have a conceptual relationship with classes and object instances that ...


12

Since most implementations take the heap and the stack from the same block of memory (growing from either end) it doesn't matter. Size is not a reason to prefer the heap over the stack. The lifetime of your object is. Should it die once it goes out of scope or not? If not, when?


11

Look at it at the other way, from the system perspective. You have a giant pool of free memory (free ram memory) where different programs can make use of. But all of their used resources should be returned sooner or later, or at least being used the entire time. When you no longer need it you should return it to the pool of free memory. Applications that ...


10

You are right that it would not be possible to make an efficient heap implementation on top of a doubly linked list. However, deques aren't doubly linked lists; they are random access containers. deques are able to swap an item in index a with index b in constant time. See the SGI documentation for deques.


10

While the other answers are giving good information about the internal details of certain various JVM heap designs, from the programmer's perspective, there is only one heap and one way to use it -- via new for allocation and via letting go for deallocation. Thus, I would say that there is no availability of different kinds or areas of a Java heap for ...


9

I just hacked up a little program that generates a set of random numbers restarting at the same seed each time, to ensure that it's "fair" and "comparable". As it goes along, it figures out the min and max of these values. And when it has generated the set of numbers, it counts how many are above the average of min and max. For VERY small arrays, it shows ...


9

I think it's easier to answer this by the order of how the memory is used. Question 3: What about the text (code) and data sections, how are they limited? Text and Data are prepared by the compiler. The requirement for the compiler is to make sure that they are accessible and pack them in the lower portion of address space. The accessible address space will ...


8

A million of characters are not to be passed by value, because passing by value (beyond a couple of words which could hold in registers) is done via the stack. And the stack space is always limited. Fortunately, when you use a QString only a small object is passed by value: the object itself uses a pointer to a memory region where the millions of bytes are ...


7

Obviously, storing X byte of data is going to reduce the amount of free memory by at least X bytes, no matter where you put it. big objects should be allocated to the heap I don't think that this is the main distinction to draw. If you need data to be accessible outside of the current stack frame (for example as a global variable, or to pass it to ...


7

Efficiency and CPU architecture. I remember when the Alpha chips came out, they could only read memory on 32 bit boundaries. If you wanted to read byte 3, you'd load a 32-bit number then have to rotate the result to isolate the byte you want. A traditional byte-by-byte string compare was quite inefficient in this architecture. That being said, I'm not sure ...


7

tl;dr summary: I couldn't find much information online. Are there different types of heap memory available in Java ? Short answer: No. Slightly longer answer: The Java Language Specification does not say anything about different types of heap memory. It doesn't say that there have to be different types, it also doesn't say that there mustn't be ...


7

The "ideal" performance of an algorithm in people's minds is dependent on what the best option is out there. If you can do a "find" operation on a data structure in O(n log N) time, is that fast enough? Maybe it is. However, for many structures you can do a find in O(n), or even O(log n), so people will call your O(n log n) "slow." This isn't because it'...


7

All of the above. It's complicated. Some CPU architectures, especially x86, include a dedicated stack pointer register and instructions that allow you to easily push/pop values on that stack. The Java Virtual Machine is a specification that defines the operations of the JVM in terms of a stack. Most instructions push and pop operands on that stack. Actual ...


6

The specification is very clear about it 2.5.3. Heap The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an ...


6

The key to resolving your problem is to return a special object on the insertion of a element. When you move the real object in the heap, you update the position in this object. To call decrease-priority you pass this object so you can go right to the position in the heap and bubble up the element if necessary. In this way it's O(log(n)). In fact finding ...


6

So the question basically is: “when should I use new?” Local variables always use stack space, but we can explicitly allocate objects on the heap with a new expression. When deciding for or against new, we do not decide based on the size of the object, as there are more important criteria: Do we statically know the size of the object? It is rarely sensible ...


6

In Java, the distinction stack vs. heap is not very meaningful, because Java doesn't give you a choice where values live. Conceptually, all objects live somewhere on the heap, but in Java an object is not the same thing as a variable. Local variables and method parameters use space on the stack, but variables never hold an object directly. The variables ...


5

Stack is per-thread, Heap is process-wide If have 100 threads all processing work items I put into a queue, exactly where do I allocate the work items such that any of the 100 threads could see them? There are other types of memory, too E.g. memory-mapped files, shared memory, I/O mapped (kernel mode). The efficiency argument is kind of moot in these ...


5

It's fairly self explanatory. The application is diminishing system memory, this memory is allocated but not useful to running code. This is analogous to a physical leak as the system memory is slowly lost (like water from a leaky bucket) to useless allocation. I'm not sure about the etymology, but it a fairly obvious analogy, so I doubt there is a clear ...


5

For the max stuff I'd use a heap, for the min a simple variable (to be potentially updated when the decrease operation lets the previous maximum fall all the way to the bottom of the heap).


5

They do share the same physical memory, however, the stack frame contains return addresses. Return addresses store the address of the code in the calling function/procedure/method, so that the called function/procedure/method knows where to go back to when it is done. Mechanically, the "return" statement issues instructions to the processor to load the ...


Only top voted, non community-wiki answers of a minimum length are eligible