DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Event-Driven Architecture Using Serverless Technologies
  • AI and Cybersecurity Protecting Against Emerging Threats
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • Personalized Code Searches Using OpenGrok

Trending

  • Event-Driven Architecture Using Serverless Technologies
  • AI and Cybersecurity Protecting Against Emerging Threats
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • Personalized Code Searches Using OpenGrok
  1. DZone
  2. Coding
  3. Java
  4. Slab: guaranteed heap alignment on the JVM

Slab: guaranteed heap alignment on the JVM

Richard Warburton user avatar by
Richard Warburton
·
Jul. 01, 13 · Interview
Like (0)
Save
Tweet
Share
7.04K Views

Join the DZone community and get the full member experience.

Join For Free

The Problem

Over time CPU clockrates have gotten considerably faster, but Memory Speed has failed to catch up. In order to make good usage of modern CPUs it's important to consider data structure alignment in memory, in order to work in sympathy with the On-CPU Cache. In other words you want good Locality of Reference.

Unfortunately the Java Platform's decision to abstract away memory allocation patterns makes it hard to guarantee properties about your memory allocation. In most situations this is a blessing: you simply don't need to worry about where your memory is coming from or going! There are some scenarios where you're implementing an algorithm that requires layout guarantees and Java falls short in this regard.

Existing Solutions

Java provides several mechanisms that offer more direct access to memory, for example the ByteBuffer and the notorious Unsafe class. Unfortunately heavy usage of these classes can quickly turn a codebase into an unstructured mess.

Recently Martin Thompson described a way of more cleanly managing these access patterns. His solution wraps up the access logic using the Flyweight Pattern. This still has the downside of having to manually implement some boilerplate and take with pointer arithmetic.

Slab: A Better Solution

Slab is an experiment to simplify the existing solutions by wrapping the boilerplate code and pointer access up into a library. The idea is simple: the user provides an interface that defines their datatype, and the library implements the underlying getters and setters via the Unsafe class. Here's a simple code example, that should demonstrate how it works:

// Define your DataType
public interface GameEvent extends Cursor {
  public int getId();
  public void setId(int value);
  public long getStrength();
  public void setStrength(long value);
}

// Create an allocator for your DataType
Allocator eventAllocator = Allocator.of(GameEvent.class);

// Allocate 100 off heap GameEvent instances - sequentially in memory
GameEvent event = eventAllocator.allocate(100);

// Move to the index of the instance that you want to read from or write to
event.move(1);

// set and get values like a normal POJO
event.setId(6);
assertEquals(6, event.getId());

Conclusions and Future Work

The source code is available on github.

This was a fun little experiment, but there's certainly a few obvious improvements that could be made:

  • Support Abstract Classes as well as Interfaces, in order to support proper classes, and not just tuple-style data structures.
  • Expose statistics about memory allocated, and allocation times - similar to GC Logs or MXBeans.
  • Support multiple flyweights for a slab of memory.
  • Experiment with concurrency access patterns - eg operations built on CAS that directly operate on the underlying memory.
  • Nested Objects.
  • Resizing slabs.
  • Investigate automated memory management - unfortunately the only way I can think of doing this is through finalizers, which have their own downsides.

I plan to implement these soon, but its always good to release open source code early and often. Any feedback is welcome.

Java (programming language) Java virtual machine

Published at DZone with permission of Richard Warburton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Event-Driven Architecture Using Serverless Technologies
  • AI and Cybersecurity Protecting Against Emerging Threats
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • Personalized Code Searches Using OpenGrok

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: