SlideShare a Scribd company logo
Handling “Out of Memory” Errors
John Tang Boyland
ECOOP EHWS, July 25, 2005
Handling “Out Of Memory” Errors
H Current advice for handling OutOfMemoryError
– Use to find memory limits.
– Don’t! There’s nothing you can do.
– . . . but Eclipse does!
H The “Hedge” technique.
– allocate a large hedge;
– free when recovering from low memory condition.
H Desiderata
– Language Specification: What is safe if memory is low?
– Compilers: Don’t move allocation later or deallocation earlier.
– Runtime: per-thread memory restrictions.
ECOOP EHWS Handling OutOfMemoryError 1
An OutOfMemoryError occurs
?
Exception thrown here
H Recovery difficult because of “low memory” condition.
H When exception is thrown, last request is not fulfilled.
ECOOP EHWS Handling OutOfMemoryError 2
Measuring Available Memory
Exception thrown and caught
Object allocated and discarded
H A loop:
– Try allocating a HUGE array;
– Catch the exception and try again with a smaller amount.
– Repeat until no exception is thrown.
H A rough underestimate of available memory.
(More accurate than Runtime.freeMemory().)
ECOOP EHWS Handling OutOfMemoryError 3
Reasoning About OutOfMemoryError
H Could occur at any time:
– even in code “proven” not to raise an exception;
– OutOfMemoryError is a subclass of Error,
(in principle) “unpredictable” and “unpreventable” errors.
H . . . well, almost any time:
– if memory needed (allocation, boxing, concatenation);
– if stack needed (call, local var. frame);
– if exception created (NPE, ArrayStoreException, etc).
H Typical advice: don’t try to handle it.
H Alternate advice: use soft/weak references.
ECOOP EHWS Handling OutOfMemoryError 4
A “Real” Program Must Handle the Error
H Almost no realistic program can provably avoid running out
of memory.
H For example: Eclipse
– uses more memory if more files are being edited;
– memory is used by many different parts (GUI, Compiler, as-
sistance, markers etc)
H Crashing on OOME is unacceptable:
– user’s work is lost, and
– workbench left (perhaps) in inconsistent state, but
– logging errors or saving files taken memory;
H The error must be handled.
ECOOP EHWS Handling OutOfMemoryError 5
Handling OutOfMemoryError in Eclipse (1 of 2)
H Eclipse catches OOME and displays warning dialog:
– but memory is low;
– dialog appears after emergency exit fails;
– otherwise only error messages on Unix stdout.
H Eclipse 3.1 uses a larger max heap size than previously
– Normally degradation (thrashing) long precedes OOME.
– Artificially lowering the heap size gets previous behavior.
ECOOP EHWS Handling OutOfMemoryError 6
Handling OutOfMemoryError in Eclipse (2 of 2)
Exception in thread "...JavaReconciler" java.lang.OutOfMemoryError
Exception in thread "...JavaReconciler" java.lang.OutOfMemoryError
Exception in thread "...JavaReconciler" java.lang.OutOfMemoryError
Error while logging event loop exception:
java.lang.OutOfMemoryError: Java heap space
Logging exception:
java.lang.OutOfMemoryError: Java heap space
Error while informing user about event loop exception:
java.lang.OutOfMemoryError: Java heap space
Dialog open exception:
java.lang.OutOfMemoryError: Java heap space
Fatal error happened during workbench emergency close.
java.lang.OutOfMemoryError: Java heap space
Unhandled event loop exception
Reason: Java heap space
H Then dialog brought up.
ECOOP EHWS Handling OutOfMemoryError 7
The “Hedge” Technique
H Pre-allocate a large area (the “hedge”);
H When OutOfMemoryError happens, release it;
H After recovery re-allocate hedge.
Recovery time
Hedge
ECOOP EHWS Handling OutOfMemoryError 8
Difficulties Using the Hedge Technique
H Need to overestimate memory required for recovery;
H Interrupted computation may leave data inconsistent;
H finally clauses before recovery may re-throw OOME;
H Error may be thrown in thread other than the “guilty” one;
H Compiler may move allocation later or deallocation earlier;
H Cannot be made automatic.
(see next slides)
ECOOP EHWS Handling OutOfMemoryError 9
One Problem Leads To Another (1 of 3)
H To avoid corruption, we introduce “finally”:
void performAction()
{
start();
doIt();
cleanup();
}
©
void performAction()
{
start();
try {
doIt();
} finally {
cleanup();
}
}
H But what if cleanup needs memory (heap/stack) ?
ECOOP EHWS Handling OutOfMemoryError 10
One Problem Leads To Another (2 of 3)
H So we pre-allocate some memory:
void performAction()
{
start();
int[] space = new int[1000];
// Point A
try {
doIt();
} finally {
// Point B
space = null;
cleanup();
}
}
H But what if the compiler . . .
– moves the allocation later (B)?
– moves the deallocation earlier (A)?
ECOOP EHWS Handling OutOfMemoryError 11
One Problem Leads To Another (3 of 3)
H Fake uses force early allocation.
H Fake tests force late deallocation.
void performAction()
{
start();
int[] space = new int[1000];
space[45] = 1+space[fact(6)];
try {
doIt();
} finally {
if (space[45] > space[44]) {
space = null;
cleanup();
}
}
}
H We have obfuscated our program.
ECOOP EHWS Handling OutOfMemoryError 12
Placing Hedge Recovery
H At outer level
+ few code changes;
+ lock state clear;
- work undone;
H Close to allocation
+ recovery fast;
- state unclear;
H If automatic, then how is recovery invoked?
– at error point, then re-entrancy problems;
– elsewhere, then finally is still an issue.
ECOOP EHWS Handling OutOfMemoryError 13
Experiences With Hedge Recovery
H Importing Java Into Internal Representation:
– Must persist in “eras”;
– As few eras as possible;
– No easy way to use weak/soft references;
H Converted JDK 1.4.2 provided source
– 4500 source files;
– 12 hours;
– 11 OutOfMemoryErrors generated;
– (300 Mb max heap on Solaris x86).
H Avoided threading issues (single-threaded code).
ECOOP EHWS Handling OutOfMemoryError 14
Conclusions
H Hedge recovery can work.
Perhaps Eclipse could use it.
H Hedge recovery would be safer if:
– Language specified what operations need memory;
– Compilers don’t move allocation/deallocation past try-finally
boundaries;
– Threads had own memory restrictions.
H Thrashing is a good alternative for interactive programs.
ECOOP EHWS Handling OutOfMemoryError 15

More Related Content

PPTX
Os Practical Assignment 1
PDF
Openframworks x Mobile
PDF
Capture and replay hardware behaviour for regression testing and bug reporting
PPTX
Exploring-javafx-3d
PDF
الكتاب المقدس العهد الجديد - رسالة بطرس الرسول الاولى
PDF
كتاب إله العهد القديم إله الدماء؟
PPTX
Mesopotamian Geo
PPSX
A tool for teaching and learning
Os Practical Assignment 1
Openframworks x Mobile
Capture and replay hardware behaviour for regression testing and bug reporting
Exploring-javafx-3d
الكتاب المقدس العهد الجديد - رسالة بطرس الرسول الاولى
كتاب إله العهد القديم إله الدماء؟
Mesopotamian Geo
A tool for teaching and learning

Viewers also liked (10)

PPTX
Everydayenglisexpressions2 091016015334-phpapp01
PDF
Michael louca thesis
PDF
سفر الخروج العهد القديم - الكتاب المقدس
PPTX
Week 3
PDF
Scanitec 2012
PDF
Sarah ساره القس الياس مقار
DOCX
Mgt. process & organizational behaviour complete
PDF
تأثير الخطية و حتمية الصليب
PDF
10.1007_s40090-015-0039-7 (1)
PDF
2. ubuntu brandmark and circle of friends
Everydayenglisexpressions2 091016015334-phpapp01
Michael louca thesis
سفر الخروج العهد القديم - الكتاب المقدس
Week 3
Scanitec 2012
Sarah ساره القس الياس مقار
Mgt. process & organizational behaviour complete
تأثير الخطية و حتمية الصليب
10.1007_s40090-015-0039-7 (1)
2. ubuntu brandmark and circle of friends
Ad

More from Jay Thakkar (7)

PDF
Mongo db m101j
PDF
O pening Files w ith LOCI Bio - Formats
PDF
Talk 8-Kevin-Imagej2
PDF
Basic image processing
PPT
Architectural Design
DOC
Learn Java 3D
PPT
Dicom standard-of-china 2011-03
Mongo db m101j
O pening Files w ith LOCI Bio - Formats
Talk 8-Kevin-Imagej2
Basic image processing
Architectural Design
Learn Java 3D
Dicom standard-of-china 2011-03
Ad

Recently uploaded (20)

PDF
Digital Strategies for Manufacturing Companies
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
assetexplorer- product-overview - presentation
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administraation Chapter 3
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
L1 - Introduction to python Backend.pptx
PDF
System and Network Administration Chapter 2
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
top salesforce developer skills in 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Digital Strategies for Manufacturing Companies
PTS Company Brochure 2025 (1).pdf.......
Operating system designcfffgfgggggggvggggggggg
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Migrate SBCGlobal Email to Yahoo Easily
assetexplorer- product-overview - presentation
Transform Your Business with a Software ERP System
System and Network Administraation Chapter 3
Design an Analysis of Algorithms II-SECS-1021-03
Computer Software and OS of computer science of grade 11.pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Design an Analysis of Algorithms I-SECS-1021-03
L1 - Introduction to python Backend.pptx
System and Network Administration Chapter 2
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Choose the Right IT Partner for Your Business in Malaysia
top salesforce developer skills in 2025.pdf
Nekopoi APK 2025 free lastest update
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf

Memory error-talk

  • 1. Handling “Out of Memory” Errors John Tang Boyland ECOOP EHWS, July 25, 2005
  • 2. Handling “Out Of Memory” Errors H Current advice for handling OutOfMemoryError – Use to find memory limits. – Don’t! There’s nothing you can do. – . . . but Eclipse does! H The “Hedge” technique. – allocate a large hedge; – free when recovering from low memory condition. H Desiderata – Language Specification: What is safe if memory is low? – Compilers: Don’t move allocation later or deallocation earlier. – Runtime: per-thread memory restrictions. ECOOP EHWS Handling OutOfMemoryError 1
  • 3. An OutOfMemoryError occurs ? Exception thrown here H Recovery difficult because of “low memory” condition. H When exception is thrown, last request is not fulfilled. ECOOP EHWS Handling OutOfMemoryError 2
  • 4. Measuring Available Memory Exception thrown and caught Object allocated and discarded H A loop: – Try allocating a HUGE array; – Catch the exception and try again with a smaller amount. – Repeat until no exception is thrown. H A rough underestimate of available memory. (More accurate than Runtime.freeMemory().) ECOOP EHWS Handling OutOfMemoryError 3
  • 5. Reasoning About OutOfMemoryError H Could occur at any time: – even in code “proven” not to raise an exception; – OutOfMemoryError is a subclass of Error, (in principle) “unpredictable” and “unpreventable” errors. H . . . well, almost any time: – if memory needed (allocation, boxing, concatenation); – if stack needed (call, local var. frame); – if exception created (NPE, ArrayStoreException, etc). H Typical advice: don’t try to handle it. H Alternate advice: use soft/weak references. ECOOP EHWS Handling OutOfMemoryError 4
  • 6. A “Real” Program Must Handle the Error H Almost no realistic program can provably avoid running out of memory. H For example: Eclipse – uses more memory if more files are being edited; – memory is used by many different parts (GUI, Compiler, as- sistance, markers etc) H Crashing on OOME is unacceptable: – user’s work is lost, and – workbench left (perhaps) in inconsistent state, but – logging errors or saving files taken memory; H The error must be handled. ECOOP EHWS Handling OutOfMemoryError 5
  • 7. Handling OutOfMemoryError in Eclipse (1 of 2) H Eclipse catches OOME and displays warning dialog: – but memory is low; – dialog appears after emergency exit fails; – otherwise only error messages on Unix stdout. H Eclipse 3.1 uses a larger max heap size than previously – Normally degradation (thrashing) long precedes OOME. – Artificially lowering the heap size gets previous behavior. ECOOP EHWS Handling OutOfMemoryError 6
  • 8. Handling OutOfMemoryError in Eclipse (2 of 2) Exception in thread "...JavaReconciler" java.lang.OutOfMemoryError Exception in thread "...JavaReconciler" java.lang.OutOfMemoryError Exception in thread "...JavaReconciler" java.lang.OutOfMemoryError Error while logging event loop exception: java.lang.OutOfMemoryError: Java heap space Logging exception: java.lang.OutOfMemoryError: Java heap space Error while informing user about event loop exception: java.lang.OutOfMemoryError: Java heap space Dialog open exception: java.lang.OutOfMemoryError: Java heap space Fatal error happened during workbench emergency close. java.lang.OutOfMemoryError: Java heap space Unhandled event loop exception Reason: Java heap space H Then dialog brought up. ECOOP EHWS Handling OutOfMemoryError 7
  • 9. The “Hedge” Technique H Pre-allocate a large area (the “hedge”); H When OutOfMemoryError happens, release it; H After recovery re-allocate hedge. Recovery time Hedge ECOOP EHWS Handling OutOfMemoryError 8
  • 10. Difficulties Using the Hedge Technique H Need to overestimate memory required for recovery; H Interrupted computation may leave data inconsistent; H finally clauses before recovery may re-throw OOME; H Error may be thrown in thread other than the “guilty” one; H Compiler may move allocation later or deallocation earlier; H Cannot be made automatic. (see next slides) ECOOP EHWS Handling OutOfMemoryError 9
  • 11. One Problem Leads To Another (1 of 3) H To avoid corruption, we introduce “finally”: void performAction() { start(); doIt(); cleanup(); } © void performAction() { start(); try { doIt(); } finally { cleanup(); } } H But what if cleanup needs memory (heap/stack) ? ECOOP EHWS Handling OutOfMemoryError 10
  • 12. One Problem Leads To Another (2 of 3) H So we pre-allocate some memory: void performAction() { start(); int[] space = new int[1000]; // Point A try { doIt(); } finally { // Point B space = null; cleanup(); } } H But what if the compiler . . . – moves the allocation later (B)? – moves the deallocation earlier (A)? ECOOP EHWS Handling OutOfMemoryError 11
  • 13. One Problem Leads To Another (3 of 3) H Fake uses force early allocation. H Fake tests force late deallocation. void performAction() { start(); int[] space = new int[1000]; space[45] = 1+space[fact(6)]; try { doIt(); } finally { if (space[45] > space[44]) { space = null; cleanup(); } } } H We have obfuscated our program. ECOOP EHWS Handling OutOfMemoryError 12
  • 14. Placing Hedge Recovery H At outer level + few code changes; + lock state clear; - work undone; H Close to allocation + recovery fast; - state unclear; H If automatic, then how is recovery invoked? – at error point, then re-entrancy problems; – elsewhere, then finally is still an issue. ECOOP EHWS Handling OutOfMemoryError 13
  • 15. Experiences With Hedge Recovery H Importing Java Into Internal Representation: – Must persist in “eras”; – As few eras as possible; – No easy way to use weak/soft references; H Converted JDK 1.4.2 provided source – 4500 source files; – 12 hours; – 11 OutOfMemoryErrors generated; – (300 Mb max heap on Solaris x86). H Avoided threading issues (single-threaded code). ECOOP EHWS Handling OutOfMemoryError 14
  • 16. Conclusions H Hedge recovery can work. Perhaps Eclipse could use it. H Hedge recovery would be safer if: – Language specified what operations need memory; – Compilers don’t move allocation/deallocation past try-finally boundaries; – Threads had own memory restrictions. H Thrashing is a good alternative for interactive programs. ECOOP EHWS Handling OutOfMemoryError 15