EagerSession.ResourceCleanupStrategy
bookmark_borderbookmark
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ควบคุมวิธีการล้างทรัพยากร TensorFlow เมื่อไม่จำเป็นต้องใช้อีกต่อไป
ทรัพยากรทั้งหมดที่จัดสรรระหว่าง EagerSession
จะถูกลบเมื่อปิดเซสชัน เพื่อป้องกันข้อผิดพลาดหน่วยความจำไม่เพียงพอ ขอแนะนำอย่างยิ่งให้ล้างข้อมูลทรัพยากรเหล่านั้นในระหว่างเซสชัน ตัวอย่างเช่น การดำเนินการ n การดำเนินการในการวนซ้ำ m จะจัดสรรทรัพยากรขั้นต่ำ n*m ในขณะที่ในกรณีส่วนใหญ่ ยังคงใช้เฉพาะทรัพยากรของการวนซ้ำครั้งล่าสุดเท่านั้น
อินส EagerSession
สามารถแจ้งเตือนได้หลายวิธีเมื่อไม่มีการอ้างอิงอ็อบเจ็กต์ TensorFlow อีกต่อไป เพื่อให้สามารถดำเนินการล้างทรัพยากรใดๆ ที่ตนเป็นเจ้าของได้
วิธีการสืบทอด
จากคลาส java.lang.Enum int สุดท้าย | เปรียบเทียบถึง (E arg0) |
ภายใน | เปรียบเทียบถึง (วัตถุ arg0) |
บูลีนสุดท้าย | เท่ากับ (วัตถุ arg0) |
คลาสสุดท้าย<E> | getDeclaringClass () |
int สุดท้าย | แฮชโค้ด () |
สตริงสุดท้าย | ชื่อ () |
int สุดท้าย | ลำดับ () |
สตริง | toString () |
คงที่ <T ขยาย Enum <T>> T | valueOf (คลาส <T> arg0, สตริง arg1) |
จากคลาส java.lang.Object บูลีน | เท่ากับ (วัตถุ arg0) |
คลาสสุดท้าย<?> | รับคลาส () |
ภายใน | แฮชโค้ด () |
โมฆะสุดท้าย | แจ้ง () |
โมฆะสุดท้าย | แจ้งทั้งหมด () |
สตริง | toString () |
โมฆะสุดท้าย | รอสักครู่ (ยาว arg0, int arg1) |
โมฆะสุดท้าย | รอ (ยาว arg0) |
โมฆะสุดท้าย | รอ () |
จากอินเทอร์เฟซ java.lang.Comparable บทคัดย่อ | CompareTo (E ขยาย Enum<E> arg0) |
ค่าแจงนับ
EagerSession.ResourceCleanupStrategy สุดท้ายแบบคงที่สาธารณะ IN_BACKGROUND
ตรวจสอบและลบทรัพยากรที่ไม่ได้ใช้ออกจากเธรดใหม่ที่ทำงานในเบื้องหลัง
นี่เป็นแนวทางที่เชื่อถือได้มากที่สุดในการล้างทรัพยากร TensorFlow โดยมีค่าใช้จ่ายในการเริ่มต้นและเรียกใช้เธรดเพิ่มเติมสำหรับงานนี้โดยเฉพาะ EagerSession
แต่ละอินสแตนซ์มีเธรดของตัวเอง ซึ่งจะหยุดเฉพาะเมื่อปิดเซสชันเท่านั้น
กลยุทธ์นี้ถูกใช้เป็นค่าเริ่มต้น
EagerSession.ResourceCleanupStrategy ON_SAFE_POINTS สุดท้ายแบบคงที่สาธารณะ
ตรวจสอบและลบทรัพยากรที่ไม่ได้ใช้ออกจากเธรดที่มีอยู่ ก่อนหรือหลังเสร็จสิ้นงานอื่น
ทรัพยากรที่ไม่ได้ใช้จะถูกปล่อยออกมาเมื่อมีการเรียกไปยังไลบรารี TensorFlow ถึงจุดที่ปลอดภัยสำหรับการล้างข้อมูล ซึ่งดำเนินการพร้อมกันและอาจบล็อกเธรดที่เรียกใช้การโทรนั้นในช่วงเวลาสั้นๆ
ควรใช้กลยุทธ์นี้เฉพาะเมื่อ ด้วยเหตุผลบางประการ ไม่ควรจัดสรรเธรดเพิ่มเติมสำหรับการล้างข้อมูล มิฉะนั้น ควรเลือกใช้ IN_BACKGROUND
EagerSession.ResourceCleanupStrategy สุดท้ายแบบคงที่สาธารณะ ON_SESSION_CLOSE
ลบทรัพยากรเมื่อปิดเซสชั่นเท่านั้น
ทรัพยากรทั้งหมดที่จัดสรรระหว่างเซสชันจะยังคงอยู่ในหน่วยความจำจนกว่าเซสชันจะถูกปิดอย่างชัดเจน (หรือผ่านเทคนิค `ลองใช้ทรัพยากร` แบบดั้งเดิม) จะไม่มีความพยายามเพิ่มเติมสำหรับการล้างทรัพยากร
กลยุทธ์นี้อาจนำไปสู่ข้อผิดพลาดหน่วยความจำไม่เพียงพอ และไม่แนะนำให้ใช้ เว้นแต่ขอบเขตของเซสชันจะถูกจำกัดให้ดำเนินการเพียงเล็กน้อยเท่านั้น
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers ส่วน Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ เนื้อหาบางส่วนได้รับอนุญาตภายใต้ใบอนุญาต NumPy
อัปเดตล่าสุด 2025-07-28 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-28 UTC"],[],[],null,["# EagerSession.ResourceCleanupStrategy\n\npublic static final enum **EagerSession.ResourceCleanupStrategy** \nControls how TensorFlow resources are cleaned up when they are no longer needed.\n\nAll resources allocated during an `EagerSession` are deleted when the session is\nclosed. To prevent out-of-memory errors, it is also strongly suggest to cleanup those resources\nduring the session. For example, executing n operations in a loop of m iterations will allocate\na minimum of n\\*m resources while in most cases, only resources of the last iteration are still\nbeing used.\n\n`EagerSession` instances can be notified in different ways when TensorFlow objects are\nno longer being referred, so they can proceed to the cleanup of any resources they owned.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Inherited Methods\n\nFrom class java.lang.Enum \n\n|----------------------------------|---------------------------------------|\n| final int | compareTo(E arg0) |\n| int | compareTo(Object arg0) |\n| final boolean | equals(Object arg0) |\n| final Class\\\u003cE\\\u003e | getDeclaringClass() |\n| final int | hashCode() |\n| final String | name() |\n| final int | ordinal() |\n| String | toString() |\n| static \\\u003cT extends Enum\\\u003cT\\\u003e\\\u003e T | valueOf(Class\\\u003cT\\\u003e arg0, String arg1) |\n\nFrom class java.lang.Object \n\n|------------------|---------------------------|\n| boolean | equals(Object arg0) |\n| final Class\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| String | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |\n\nFrom interface java.lang.Comparable \n\n|--------------|-------------------------------------|\n| abstract int | compareTo(E extends Enum\\\u003cE\\\u003e arg0) |\n\nEnum Values\n-----------\n\n#### public static final EagerSession.ResourceCleanupStrategy\n**IN_BACKGROUND**\n\nMonitor and delete unused resources from a new thread running in background.\n\nThis is the most reliable approach to cleanup TensorFlow resources, at the cost of\nstarting and running an additional thread dedicated to this task. Each `EagerSession`\ninstance has its own thread, which is stopped only when the session is closed.\n\nThis strategy is used by default.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n#### public static final EagerSession.ResourceCleanupStrategy\n**ON_SAFE_POINTS**\n\nMonitor and delete unused resources from existing threads, before or after they complete\nanother task.\n\nUnused resources are released when a call to the TensorFlow library reaches a safe point\nfor cleanup. This is done synchronously and might block for a short period of time the thread\nwho triggered that call.\n\nThis strategy should be used only if, for some reasons, no additional thread should be\nallocated for cleanup. Otherwise, [IN_BACKGROUND](/versions/r2.8/api_docs/java/org/tensorflow/EagerSession.ResourceCleanupStrategy#IN_BACKGROUND) should be preferred.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n#### public static final EagerSession.ResourceCleanupStrategy\n**ON_SESSION_CLOSE**\n\nOnly delete resources when the session is closed.\n\nAll resources allocated during the session will remained in memory until the session is\nexplicitly closed (or via the traditional \\`try-with-resource\\` technique). No extra task for\nresource cleanup will be attempted.\n\nThis strategy can lead up to out-of-memory errors and its usage is not recommended, unless\nthe scope of the session is limited to execute only a small amount of operations.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e"]]