正解:B
* TheLimitsclass in Apex provides methods to check the consumption of governor limits, such as the number of SOQL queries, DML statements, and heap size used.
* Example:
Integer dmlStatements = Limits.getDMLStatements();
Integer dmlLimit = Limits.getLimitDMLStatements();
* Not Suitable:
* Option A:OrgLimitsis not an Apex class.
* Option C:Messagingdeals with email and messaging operations.
* Option D:Exceptionis for handling exceptions, not tracking resource usage.
:Limits Class
Reference:Apex Developer Guide - Limits Class
This concept falls under"Testing, Debugging, and Deployment"(22% weight) in the PD1 guide, as developers need to monitor and test for governor limits to ensure Apex code scalability and reliability.
最新のコメント (最新のコメントはトップにあります。)
正解は **B. 制限 (Limits)** です。
Apexにおいて、現在のトランザクションで使用されているリソース量(ガバナ制限の消費状況)を確認するために使用するクラスは `System.Limits` クラスです。
解説と、他の選択肢との違いをまとめます。
### 正解の解説
**B. 制限 (Limits)**
`System.Limits` クラスには、現在の実行コンテキストで使用されたリソースの量や、その制限値(上限)を返すメソッドが用意されています。
* **使用された量を返すメソッド:** `get` で始まります。
* `Limits.getDmlStatements()`: 実行されたDMLステートメントの数
* `Limits.getSoqlQueries()`: 発行されたSOQLクエリの数
* `Limits.getHeapSize()`: 使用されているヒープサイズ
* **上限(限界値)を返すメソッド:** `getLimit` で始まります。
* `Limits.getLimitDmlStatements()`: DMLステートメントの最大許容数(例: 150)
開発者はこれらを使用して、「あとどれくらいクエリを投げられるか」をコード内でチェックし、制限エラー(System.LimitException)を防ぐロジックを書くことができます。
-----
### 不正解の解説
**A. 組織制限 (OrgLimits)**
`System.OrgLimits` というクラスは存在しますが、これは**組織全体**の制限(例:24時間のAPIリクエスト総数やデータストレージ容量など)を確認するためのものです。1回のトランザクション内でのDML数などのガバナ制限を確認するものではありません。
**C. メッセージング (Messaging)**
`Messaging` 名前空間は、メールの送信(`SingleEmailMessage`など)やプッシュ通知など、通信に関連する機能を提供します。リソース監視とは無関係です。
**D. 例外 (Exception)**
`Exception` クラスはエラー処理に使用されます。ガバナ制限を超えた場合に例外(`LimitException`)が発生しますが、Exceptionクラス自体がリソースの使用量を測定したり返したりするわけではありません。
-----
### コード例
実際に開発で使用するイメージは以下のようになります。
```apex
// 現在のDML使用量を取得
Integer usedDML = Limits.getDmlStatements();
// DMLの最大制限数を取得
Integer maxDML = Limits.getLimitDmlStatements();
System.debug('現在使用中のDML数: ' + usedDML);
System.debug('DMLの残り回数: ' + (maxDML - usedDML));
```...