Giter Club home page Giter Club logo

unsafe-array's Introduction

Unsafe Array ☝️↔️

A memory-unsafe array implementation that can contain Long.MAX_VALUE / SIZEOF_PTR[1] elements, or as many as possible before running out of memory.

An unsafe array is similar to an Object[]. The default value is of an element is null, and the element at an index can be written and read with the set() and get() methods. An unsafe array can also be manually resized with the resize() method, but is otherwise fixed-size.

Memory allocated to an unsafe array can never be freed by the garbage collector. To prevent a memory leak, it must be manually destroyed with the delete() method.

Attempting to access an index out of bounds or perform any operation on a destroyed unsafe array yields undefined results. Accessing unallocated memory will crash the JVM with a segmentation fault.

[1] SIZEOF_PTR is 4 for 32-bit JVM architectures and 8 for 64-bit.

Example

import dog.silly.util.UnsafeArray;

public class Example {
    private static final long SIZE = (long) Integer.MAX_VALUE + 1;

    public static void main(String[] args) {
        // Unsafe arrays can be longer than native arrays or Collections.
        UnsafeArray<Object> array = new UnsafeArray<>(SIZE);

        // This will take a while... or you might run out of memory.
        for (long i = 0; i < array.length(); i++)
            array.set(i, new Object());

        System.out.println(array);

        // Destroy the array once it is no longer needed.
        array.delete();
    }
}

unsafe-array's People

Contributors

arthu-rguo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.