-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-21860][core]Improve memory reuse for heap memory in HeapMemoryAllocator
#19077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,9 +46,12 @@ private boolean shouldPool(long size) { | |
|
||
@Override | ||
public MemoryBlock allocate(long size) throws OutOfMemoryError { | ||
if (shouldPool(size)) { | ||
int numWords = (int) ((size + 7) / 8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add some check to make sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L51: assert (alignedSize >= size); |
||
long alignedSize = numWords * 8L; | ||
assert (alignedSize >= size); | ||
if (shouldPool(alignedSize)) { | ||
synchronized (this) { | ||
final LinkedList<WeakReference<long[]>> pool = bufferPoolsBySize.get(size); | ||
final LinkedList<WeakReference<long[]>> pool = bufferPoolsBySize.get(alignedSize); | ||
if (pool != null) { | ||
while (!pool.isEmpty()) { | ||
final WeakReference<long[]> arrayReference = pool.pop(); | ||
|
@@ -62,11 +65,11 @@ public MemoryBlock allocate(long size) throws OutOfMemoryError { | |
return memory; | ||
} | ||
} | ||
bufferPoolsBySize.remove(size); | ||
bufferPoolsBySize.remove(alignedSize); | ||
} | ||
} | ||
} | ||
long[] array = new long[(int) ((size + 7) / 8)]; | ||
long[] array = new long[numWords]; | ||
MemoryBlock memory = new MemoryBlock(array, Platform.LONG_ARRAY_OFFSET, size); | ||
if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) { | ||
memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_CLEAN_VALUE); | ||
|
@@ -98,12 +101,13 @@ public void free(MemoryBlock memory) { | |
long[] array = (long[]) memory.obj; | ||
memory.setObjAndOffset(null, 0); | ||
|
||
if (shouldPool(size)) { | ||
long alignedSize = ((size + 7) / 8) * 8; | ||
if (shouldPool(alignedSize)) { | ||
synchronized (this) { | ||
LinkedList<WeakReference<long[]>> pool = bufferPoolsBySize.get(size); | ||
LinkedList<WeakReference<long[]>> pool = bufferPoolsBySize.get(alignedSize); | ||
if (pool == null) { | ||
pool = new LinkedList<>(); | ||
bufferPoolsBySize.put(size, pool); | ||
bufferPoolsBySize.put(alignedSize, pool); | ||
} | ||
pool.add(new WeakReference<>(array)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need to create a new method:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I aggree with you .
I hava updated,thanks