When this structure is allocated, how much memory is wasted?

DanielV

I'm building an index tree based on a bplus tree in Rust and have so far used a definition for parent nodes as:

struct Parent<T : std::fmt::Debug> {
    subtree_count : [usize; Arity],
    children      : [*mut ParentOrLeaf<T>; Arity],
    used          : usize, 
}

On my 64 bit computer with Arity=8 that works out to a total memory requirement of 136 bytes. I'm using std::alloc::Layout::new and std::alloc::alloc to allocate this structure. But I'm worried that being just slightly larger than a power of two (136 > 128) that the malloc library will end up allocating 256 bytes for this data structure instead of just 136. Since this is a container type, wasting half the memory allocated is unacceptable.

std::alloc::Layout::new::<Parent<T>>().align() reports a layout of 8 as expected.

How much memory will this structure actually take up when it is allocated?

If so much memory is wasted, I could change subtree_count : [usize; Arity] to subtree_count : [usize; Arity-1], which would make the total memory 128. Then redo all of the optimized logic of my library to handle the change. But before I do, I want to make sure that is actually necessary.

prog-fh

If the size is 136 this means that a contiguous allocation of many structs in an array or a vector will use exactly 136 bytes for each struct.

When it comes to individually allocating some structs, the amount of wasted space only depends on the underlying malloc() strategy, but is not a property of the allocated type.

For example, a quick and dirty adaptation of your example on my stable-x86_64-unknown-linux-gnu platform gives this :

size 136
align 8
arr delta1 136
arr delta2 136
box delta1 144
box delta2 144

Of course there is no guaranty that the three allocated structs are near to each other, but in this specific case they are, and the wasted (not really wasted, but used by the allocator itself) space is 8 bytes.

struct ParentOrLeaf<T: std::fmt::Debug> {
    value: Option<T>,
}

const Arity: usize = 8;

struct Parent<T: std::fmt::Debug> {
    subtree_count: [usize; Arity],
    children: [*mut ParentOrLeaf<T>; Arity],
    used: usize,
}

fn main() {
    type P = Parent<i32>;
    let l = std::alloc::Layout::new::<P>();
    println!("size {}", l.size());
    println!("align {}", l.align());
    let ptr: *mut ParentOrLeaf<i32> = std::ptr::null_mut();
    let arr = [
        P {
            subtree_count: [0; Arity],
            children: [ptr; Arity],
            used: 0,
        },
        P {
            subtree_count: [0; Arity],
            children: [ptr; Arity],
            used: 0,
        },
        P {
            subtree_count: [0; Arity],
            children: [ptr; Arity],
            used: 0,
        },
    ];
    let a0 = &arr[0] as *const P as usize;
    let a1 = &arr[1] as *const P as usize;
    let a2 = &arr[2] as *const P as usize;
    println!("arr delta1 {}", a1 - a0);
    println!("arr delta2 {}", a2 - a1);
    let p0 = Box::new(P {
        subtree_count: [0; Arity],
        children: [ptr; Arity],
        used: 0,
    });
    let p1 = Box::new(P {
        subtree_count: [0; Arity],
        children: [ptr; Arity],
        used: 0,
    });
    let p2 = Box::new(P {
        subtree_count: [0; Arity],
        children: [ptr; Arity],
        used: 0,
    });
    let a0 = p0.as_ref() as *const P as usize;
    let a1 = p1.as_ref() as *const P as usize;
    let a2 = p2.as_ref() as *const P as usize;
    println!("box delta1 {}", a1 - a0);
    println!("box delta2 {}", a2 - a1);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How much memory is allocated to this Bitmap?

From Dev

How much memory is allocated to an object of this class?

From Dev

How much memory and vcore allocated on hadoop YARN?

From Dev

NSMutableArray arrayWithCapacity - how much memory is allocated?

From Dev

How is it decided that how much physical memory is to be allocated to java heap?

From Dev

how much memory is a stack object allocated upon creation?

From Dev

Will memory be allocated for a typedef structure pointer?

From Dev

why does tensorflow allocate more memory than requested in gpu? Is there any function to determine how much the memory allocated?

From Dev

when is the memory allocated for programs?

From Dev

When is a memory allocated to a variable

From Dev

C alloca function - what happens when too much memory is tried to be allocated

From Dev

How much space is allocated on the stack

From Dev

Need help to produce correct syntax to free memory for char** when pointer to allocated memory was assign to structure member

From Dev

How is memory allocated when you create a property of class?

From Dev

How is memory allocated when you create a property of class?

From Dev

When is the memory allocated by glBufferData freed?

From Dev

How is memory allocated in int array

From Dev

How to extend the allocated memory of a bytebuffer

From Dev

How memory is allocated to macros in c?

From Dev

How are struct members allocated in memory?

From Dev

How to correctly count allocated memory

From Dev

How are struct members allocated in memory?

From Dev

How to check if allocated memory was freed?

From Dev

Write structure to a file && read structure to a dynamicly allocated memory

From Dev

Correct way to free memory for a structure that contains allocated memory referenced by pointers

From Dev

When does memory gets allocated for a variable in c?

From Dev

When memory is allocated to a function (definition or call)

From Java

Why is stack memory allocated when it is not used?

From Dev

where is placed the this pointer and and when memory is allocated for it?

Related Related

  1. 1

    How much memory is allocated to this Bitmap?

  2. 2

    How much memory is allocated to an object of this class?

  3. 3

    How much memory and vcore allocated on hadoop YARN?

  4. 4

    NSMutableArray arrayWithCapacity - how much memory is allocated?

  5. 5

    How is it decided that how much physical memory is to be allocated to java heap?

  6. 6

    how much memory is a stack object allocated upon creation?

  7. 7

    Will memory be allocated for a typedef structure pointer?

  8. 8

    why does tensorflow allocate more memory than requested in gpu? Is there any function to determine how much the memory allocated?

  9. 9

    when is the memory allocated for programs?

  10. 10

    When is a memory allocated to a variable

  11. 11

    C alloca function - what happens when too much memory is tried to be allocated

  12. 12

    How much space is allocated on the stack

  13. 13

    Need help to produce correct syntax to free memory for char** when pointer to allocated memory was assign to structure member

  14. 14

    How is memory allocated when you create a property of class?

  15. 15

    How is memory allocated when you create a property of class?

  16. 16

    When is the memory allocated by glBufferData freed?

  17. 17

    How is memory allocated in int array

  18. 18

    How to extend the allocated memory of a bytebuffer

  19. 19

    How memory is allocated to macros in c?

  20. 20

    How are struct members allocated in memory?

  21. 21

    How to correctly count allocated memory

  22. 22

    How are struct members allocated in memory?

  23. 23

    How to check if allocated memory was freed?

  24. 24

    Write structure to a file && read structure to a dynamicly allocated memory

  25. 25

    Correct way to free memory for a structure that contains allocated memory referenced by pointers

  26. 26

    When does memory gets allocated for a variable in c?

  27. 27

    When memory is allocated to a function (definition or call)

  28. 28

    Why is stack memory allocated when it is not used?

  29. 29

    where is placed the this pointer and and when memory is allocated for it?

HotTag

Archive