简体   繁体   中英

Arrays and Linked List : will arrays be able to allocate 300MB in memory if 512 MB is free but 300 MB is not contiguous

I have a conceptual question regarding arrays and linked lists.

Arrays are a contiguous block of memory.

Suppose a system has 1024 units of memory. 512 units are occupied (system+other).

Our process needs 300 units.

In memory while 512 units are available, the maximum contiguous block is say 200 units. So there could be 1 block of 200 units, 3 blocks of 100.

  1. What will happen to array allocation in a language such as Java. In C, malloc will supposedly fail?.
  2. If the underlying data structure is Linked List, it should be able to handle the non-contiguous nature with ease (Am I correct in this assumption)?

  3. Will the OS "defrag" the memory or whatever is the equivalent?

By memory, I mean RAM. Not hard disk.

Well, it's somewhat implementation-specific, but I'd expect a JVM implementation to initialize arrays by allocating one contiguous block of memory. I don't think it's absolutely required, but it's going to be by far the most efficient approach.

If the memory is unavailable in the form required, the JVM will throw an OutOfMemoryError , as per section 15.10.2 of the JLS:

Next, space is allocated for the new array. If there is insufficient space to allocate the array, evaluation of the array creation expression completes abruptly by throwing an OutOfMemoryError.

(The JVM should try to arrange enough space first, however - running garbage collection and compaction before throwing the error, for example.)

Yes, a linked list may cope with this - but it will require more memory anyway, because of all the extra objects being created. If your 300MB of memory requirement is actually 300 million bytes, and you create a LinkedList<Byte> , trying to create 300 million nodes will fail because of the overhead of all the objects and references.

As for "defragging" - many JVMs have "compacting" garbage collectors, which move objects around in memory in order to avoid fragmentation. Don't forget this is all virtual memory - it doesn't need to be physically contiguous. That part is dealt with by the operating system at a level we rarely need to worry about.

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