我有一些 ASP.NET 核心项目,我正在查看消耗的内存。我只是被它吓坏了。这不是一个大项目。我有几个 Controller ,它消耗了大约 350MB 的内存,这对于网络服务器来说是一个很大的内存量。
我的问题是我可以以某种方式减少它吗?我的一个想法是给你结构而不是数据关系模型的模型,但这不会像我想要的那样减少那个麻烦。还有其他一些方法,我喜欢全部尝试:)
最后一件事,我现在 ASP.NET 核心是一个非常复杂的框架,这意味着我不会将它用于小型项目,这仅用于教育目的。最后一个问题:内存是大问题吗?
我的项目截图:
我更大的 Controller :)
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RustWiki.Data;
using RustWiki.Models.WikiViewModels;
namespace RustWiki.Controllers.api
{
[Produces("application/json")]
public class TableManagerController : Controller
{
private readonly ApplicationDbContext _context;
public TableManagerController(ApplicationDbContext context)
{
_context = context;
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
base.Dispose(disposing);
}
//Get
[HttpGet]
[Route("/api/ArmorTypes")]
public IActionResult GetArmorTypes() => Ok(_context.ArmorTypes.ToList());
[HttpGet]
[Route("/api/Cloating")]
public IActionResult GetCloating() => Ok(_context.Cloating.Include(c => c.ArmorType).Include(c => c.Defencis).Include(c => c.Item.ItemType).ToList());
[HttpGet]
[Route("/api/Defencis")]
public IActionResult GetDefences() => Ok(_context.Defencis.ToList());
[HttpGet]
[Route("/api/Dmgs")]
public IActionResult GetDmg() => Ok(_context.Dmgs.ToList());
[HttpGet]
[Route("/api/Equipment")]
public IActionResult GetEquipment() => Ok(_context.Equipment.Include(e => e.Dmg).Include(e => e.Item.ItemType).ToList());
[HttpGet]
[Route("/api/Items")]
public IActionResult GetItems() => Ok(_context.Items.Include(i => i.ItemType).ToList());
[HttpGet]
[Route("/api/ItemTypes")]
public IActionResult GetItemType() => Ok(_context.ItemTypes.ToList());
[HttpGet]
[Route("/api/Resources")]
public IActionResult GetResource() => Ok(_context.Resources.ToList());
//Delete
[HttpDelete]
[Route("/api/ArmourTypes/{id}")]
public IActionResult DelArmourType(int id) => Delete(_context.ArmorTypes, id);
[HttpDelete]
[Route("/api/Cloating/{id}")]
public IActionResult DelCloating(int id) => Delete(_context.Cloating, id);
[HttpDelete]
[Route("/api/Defences/{id}")]
public IActionResult DelDefencis(int id) => Delete(_context.Defencis, id);
[HttpDelete]
[Route("/api/Dmgs/{id}")]
public IActionResult DelDmg(int id) => Delete(_context.Dmgs, id);
[HttpDelete]
[Route("/api/Equipments/{id}")]
public IActionResult DelEquipment(int id) => Delete(_context.Equipment, id);
[HttpDelete]
[Route("/api/Items/{id}")]
public IActionResult DelItem(int id) => Delete(_context.Items, id);
[HttpDelete]
[Route("/api/ItemTypes/{id}")]
public IActionResult DelItemType(int id) => Delete(_context.ItemTypes, id);
[HttpDelete]
[Route("/api/Resources/{id}")]
public IActionResult DelResource(int id) => Delete(_context.Resources, id);
private IActionResult Delete<T>(DbSet<T> set, int id) where T : class
{
var obj = set.SingleOrDefault(delegate (T o)
{
return (o as IWikiModel)?.Id == id;
});
if (obj == null)
return NotFound();
set.Remove(obj);
_context.SaveChanges();
return Ok(obj);
}
//Create
[HttpPost]
[Route("/api/ArmourType")]
public IActionResult CreateArmourType([FromBody]ArmorType type)
{
return Ok();
}
}
}
感谢所有的答案。
最佳答案
任何框架对于新手来说都是复杂的。话虽如此,与完整框架相比,.NETCore 是一个更轻量和简化的运行时,使其更适用于小型项目。
当您说您的 API 消耗大量内存时,是真的吗?你目前在做什么基准测试?您是否在本地运行它并查看内存消耗?如果是这样,在 Debug
中还有很多东西在运行。不仅仅是像调试符号这样的应用程序代码,它们会增加看似很小的应用程序的内存占用。
如果您确实需要寻找内存泄漏或高消耗(这是特定于您的情况的指标),请使用经过试验和测试的分析器,例如 ANTS Profiler .
关于asp.net-core - ASP.NET 核心高内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371025/