Presentation is loading. Please wait.

Presentation is loading. Please wait.

111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.

Similar presentations


Presentation on theme: "111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science."— Presentation transcript:

1 111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science

2 222 Contents IntroductionIntroduction –Heap and garbage collection Memory area classesMemory area classes –HeapMemory, ImmortalMemory, ScopedMemory Estimating sizesEstimating sizes Threads and memory usageThreads and memory usage –Memory Parameters, No heap threads and handlers Physical memory usePhysical memory use –Raw memory access SummarySummary Book: Wellings 7.3, 8 (part), 15.1 & 15.2 (part)Book: Wellings 7.3, 8 (part), 15.1 & 15.2 (part)

3 33 Introduction In regular JavaIn regular Java –All objects are allocated on the heap A single expandable area of memoryA single expandable area of memory Local variables and parameters are on the thread’s stackLocal variables and parameters are on the thread’s stack –In Java these can only be primitive types and references! –A garbage collection algorithm runs in the background E.g. when heap is full, periodically or incrementallyE.g. when heap is full, periodically or incrementally Releases/recycles objects which are no longer reachableReleases/recycles objects which are no longer reachable

4 44 Realtime issues Memory may be limited – –May need special management Garbage collection may interfere with processes – –Introducing additional delays – –Causing missed deadlines

5 55 RTSJ responses Allows objects to be allocated in non-heap “memory areas” – –Avoiding garbage collection for those areas And/or recycling the whole area as one (i.e. scoped memory) – –Allowing more explicit management E.g. monitoring space used and free

6 66 Memory area classes > javax.realtime. MemoryArea javax.realtime. HeapMemory extends > javax.realtime. ScopedMemory javax.realtime. ImmortalMemory javax.realtime. LTMemory javax.realtime. VTMemory

7 77 Memory area types HeapMemory – –The regular Java heap ImmortalMemory – –Single area of non-GC memory ScopedMemory – –Block(s) of memory which are reference counted and allocated/freed as a whole – –LTMemory – ScopedMemory with allocation time linearly proportional to size – –VTMemory – ScopedMemory with variable allocation time

8 88 MemoryArea class [optional detail] package javax.realtime; public abstract class MemoryArea { … public long size(); public long memoryConsumed(); public long memoryRemaining(); … Monitor/manage usage

9 99 … public void enter(Runnable logic); public void executeInArea(Runnable logic); … public Object newInstance(Class clazz); public Object newArray(Class type, int number); … } Explicit allocation within currently valid memory area Manipulate default memory area used for realtime thread’s memory allocation [optional detail]

10 10 ImmortalMemory & HeapMemory [optional detail] package javax.realtime; public abstract class ImmortalMemory extends MemoryArea { public static ImmortalMemory instance(); } public abstract class HeapMemory extends MemoryArea { public static HeapMemory instance(); } Singletons

11 11 Example [optional detail] … ImmortalMemory.instance().executeInArea( new Runnable() { public void run() { int a[] = new int[1000]; … } }); … int a[] = (int[])ImmortalMemory.instance(). newArray(Integer.TYPE, 1000); … In immortal memory: never garbage collected

12 12 Scoped Memory Block of memory managed as a whole Accessible only to realtime threads (and asynchronous event handlers) Accessible only to a realtime thread that has enter ed it Released (and objects finalised) when all realtime threads leave it – –i.e. “garbage collected” as a single unit – –References to it are not allowed where they might “outlive” it, e.g. on the heap or immortal memory

13 13 LTMemory & VTMemory [optional detail] package javax.realtime; public abstract class LTMemory extends ScopedMemory { public LTMemory(long size); public LTMemory(long initial, long maximum); … } public abstract class VTMemory extends ScopedMemory { public VTMemory(long size); public VTMemory(long initial, long maximum); … }

14 14 Example [optional detail] … LTMemory mem = new LTMemory(size); mem.enter( new Runnable() { public void run() { int a[] = new int[1000]; … } }); … In scoped memory ‘mem’ Needs to be big enough! Scoped memory backing store released when method finishes

15 15 Scoped memory notes [optional detail] References to objects in scoped memory – –Cannot be on heap – –Cannot be in immortal memory – –Cannot be in a scoped memory that is “below” the scoped memory containing the object Each scoped memory has one parent scoped memory = the scoped memory from which it was entered

16 16 Scoped memory portals [optional detail] Each ScopedMemory can have one “portal” – –An object, typically in the scoped memory, that can be accessed from the ScopedMemory object Note that the ScopedMemory object itself may be on the Heap, in immortal memory, etc.; its backing store is separate from the object – –Allows different threads to find an entry to object(s) in a common ScopedMemory The reference rules make this difficult otherwise – –… public Object getPortal(); public void setPortal(Object o); …

17 17 Memory area stack RealtimeThread and AsyncEventHandler have a MemoryArea parameter – –Initial memory area in which to execute – –Available via getMemoryArea() Memory areas are also visible as a memory area stack – –See RealtimeThread static methods e.g. getCurrentMemoryArea(), getOuterMemoryArea(int index)

18 18 Estimating sizes Scoped memory areas need to have a size specifiedScoped memory areas need to have a size specified –Supported by SizeEstimator …

19 19 SizeEstimator class [optional detail] package javax.realtime; public class SizeEstimator { public SizeEstimator(); // call as required public void reserve(Class c, int number); public void reserve(SizeEstimator s, int number); public void reserveArray(int dimensions, Class type); … public long getEstimate(); } Adds to running total Current estimate (sufficient)

20 20 Example [optional detail] … SizeEstimator s = new SizeEstimator(); s.reserve(MyClass.class, 10); … long size = s.getEstimate(); … Enough for 10 instances of MyClass (excluding any member reference Values!)

21 21 Memory ParametersMemory Parameters Parameter to RealtimeThread and AsyncEventHandlerParameter to RealtimeThread and AsyncEventHandler –Control memory use of the task Specifies:Specifies: –maxMemoryArea – maximum amount of memory allocation allowed in the task’s (initial) memory area –maxImmortal – maximum amount of memory allocation allowed in immortal memory –allocationRate – maximum rate at which heap memory can be allocated [optionally enforced] 21

22 22 MemoryParameters class package javax.realtime; public class MemoryParameters { public static final long NO_MAX; public MemoryParameters( long maxMemoryArea, long maxImmortal, long allocationRate); public long getMaxMemoryArea(); public long SetMaxMemoryAreaIfFeasible(); … }

23 23 Memory Parameter checking May be used by Scheduler for admission control (feasibility checking) – –E.g. total available memory Checked/enforced by new – –Throws OutOfMemoryError if exceeded E.g. – –too much (immortal or scoped) memory used – –Heap memory allocated too rapidly May impact need for garbage collection Implementation dependent whether this is checkedImplementation dependent whether this is checked

24 24 No heap threads and handlersNo heap threads and handlers To completely avoid interference from the garbage collector a Schedulable mustTo completely avoid interference from the garbage collector a Schedulable must –Not use heap memory at all Indicated to JVM by using NoHeapRealtimeThread class or AsyncEventHandler with noheap = trueIndicated to JVM by using NoHeapRealtimeThread class or AsyncEventHandler with noheap = true Throws exception at run-time if heap memory usedThrows exception at run-time if heap memory used –Have a higher priority than any heap using thread Garbage collection may occur on behalf of any heap using thread – at its priorityGarbage collection may occur on behalf of any heap using thread – at its priority 24

25 25 Physical memory access An embedded device may have several different kinds of memory with different characteristics – –Fast volatile DRAM/SRAM – –Fast readonly ROM/EPROM – –Slower non-volatile EEPROM/FLASH – –Removable memory, e.g. SD/MMC/… The program may need to use the right kind for the right things…

26 26 Example Physical Memory map From Wellings Figure 15.2 © Wellings 2004 IO Memory Shared memory Removable Memory RAM ROM High address Low address

27 27 Raw Memory Access Can access raw bytes, e.g. – –Interacting with an IO device (see later notes) – –Accessing memory shared with another (esp. non-Java) application Via RawMemoryAccess …

28 28 RawMemoryAccess class package javax.realtime; public class RawMemoryAccess { public RawMemoryAccess( Object type, long base, long size); … public byte getByte(long offset); public int getInt(long offset); public void setByte(long offset, byte value); public void setInt(long offset, int value); … } Raw memory bytes Byte order platform dependent… Types: ALIGNED BYTESWAP DMA IO_PAGE SHARED … (see PhysicalMemoryManager)

29 29 Checking byte order package javax.realtime; public class RealtimeSystem { … public static final byte BIG_ENDIAN; public static final byte LITTLE_ENDIAN; public static final byte BYTE_ORDER; … } LITTLE_ENDIAN or BIG_ENDIAN as appropriate i.e. start at the “big” or “little” end of the number

30 30 Physical memory areas Can also create and use MemoryAreas that are “locked” to particular parts of physical memory – –to ensure correct physical memory is used E.g. EEPROM/FLASH vs RAM vs DMA Extends previous MemoryArea classes… – –Inherits basic characteristics and API – –Needs extra information about physical “backing”

31 31 Physical Memory classes > javax.realtime. MemoryArea > javax.realtime. ScopedMemory extends javax.realtime. ImmortalPhysicalMemory javax.realtime. LTPhysicalMemory javax.realtime. VTPhysicalMemory

32 32 E.g. ImmortalPhysicalMemory package javax.realtime; public class ImmortalPhysicalMemory extends MemoryArea { public ImmortalPhysicalMemory( Object type, long base, long size); … } Types: ALIGNED BYTESWAP DMA IO_PAGE SHARED … (see PhysicalMemoryManager)

33 33 Summary Normal Java object allocated on the heapNormal Java object allocated on the heap –Released when unreachable by garbage collector Realtime and embedded systems mayRealtime and embedded systems may –Need more explicit management of space –Be delayed unpredictably by garbage collection RTSJ allows realtime threads to use different memory areas:RTSJ allows realtime threads to use different memory areas: –HeapMemory – normal heap –ImmortalMemory – non-garbage collected area –ScopedMemory …

34 34 Summary (2) Scoped memoryScoped memory –LTMemory or VTMemory –memory areas which are released as a whole when no realtime threads are using them –May be nested –Cannot have references to object in scoped memory on heap, immortal memory or parent/ancestor scoped memory Estimating sizes for scoped memory areas – using SizeEstimatorEstimating sizes for scoped memory areas – using SizeEstimator

35 35 Summary (3) Realtime thread and asycn event handler memory usage limits specified by MemoryParameters : – –Memory area and immortal memory size limites – –Heap allocation rate limit (optionally checked) Can avoid interference from GC using NoHeapRealtimeThread or noheap AsyncEventHandler – –Iff priority > any heap-using thread / handler

36 36 Summary (4) Access to raw physical memory – –E.g. for device IO or inter-program communication – –via RawMemoryAccess Use of specific areas of physical memory – –E.g. to reflect type(s) of memory present – –via PhysicalImmortalMemory, LTPhysicalMemory & VTPhysicalMemory


Download ppt "111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science."

Similar presentations


Ads by Google